unfortunately has stopped

《煮酒论Android》第五章,由Button启动TextView完整程序:

text.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
    <TextView
        android:id="@+id/text_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="16sp"
        android:textColor="#ffffff"
        android:padding="10dp"
        android:background="#cc0000"
        android:text="这是一个TextView, 这段文字来源于txet.xml布局文件。"/>   

</LinearLayout>

 

ViewTextActivity.java:

package com.example.chapter05;

import android.app.Activity;

import android.os.Bundle;
import android.widget.TextView;

public class ViewTextActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setTitle("ViewTextActivity");
        setContentView(R.layout.text);
        findAndModifyTextView();
    }
   
    private void findAndModifyTextView(){
     
     TextView textView = (TextView)findViewById(R.id.text_view);
     CharSequence textViewOld = textView.getText();
     textView.setText("Text.xml文件里配置的文字是:"
        + textViewOld
        +"\n 在此我们动态修改为:textView的值也是可以动态修改的。");
    }
}

 

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
   <Button
   android:id="@+id/text_view_button"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:text="TextView"/>
  
</LinearLayout>

 

MainActivity.java

package com.example.chapter05;

import android.app.Activity;

import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.content.Intent;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       
        setTitle("ViewTextActivity");
        setContentView(R.layout.main);
        Button textViewButton = (Button)findViewById(R.id.text_view_button);
        textViewButton.setOnClickListener(mTextViewButtonListener);
    }
   
    private Button.OnClickListener mTextViewButtonListener = new Button.OnClickListener(){
     public void onClick(View v){
      Intent intent = new Intent (MainActivity.this, ViewTextActivity.class);
      startActivity(intent);
     }
     
    };
}

 

AndroidMainifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chapter05"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
           
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
       
        <activity
            android:name=".ViewTextActivity"
            android:label="@string/app_name">
        </activity>
       
    </application>

</manifest>

 

注意:

(1)android:id="@+id/text_view_button"中"@+id/text_view_button"里不能有空格;

(2)两个Activity都需要在Mainifest.xml中声明;

 

posted on 2015-04-27 21:58  深泉  阅读(375)  评论(0编辑  收藏  举报