package test2.com; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; public class Test2Activity extends Activity { /** Called when the activity is first created. */ //置于一个全局的TextView, public TextView tv; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button start = (Button)findViewById(R.id.button1); Button finish = (Button)findViewById(R.id.button2); //把界面中的textView赋给tv tv = (TextView)findViewById(R.id.textView2); TextView tv1 = (TextView)findViewById(R.id.textView2); start.setOnClickListener(new OnClickListener(){ public void onClick(View v) { // TODO Auto-generated method stub //此时的tv是全局的, tv.setText("start"); //tv1 在onCreate中产生的,所以下面的语句会抱错!! //------------------// //tv1.setText("inner Text"); //-------------------// } }); // finish.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub tv.setText("finish"); } }); } }
以上程序主代码
在res/main.xml中加一个textview,2个button,分别设置text为start,stop
点击start,stop分别显示start,stop ,~~~~~~~