unfortunately has stopped

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

text.xml:


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
            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布局文件。"/>   

 

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:


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
  

转载于:https://www.cnblogs.com/deepsprings/p/DeepSpings.html

你可能感兴趣的:(unfortunately has stopped)