1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
package com.pandy.tl; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TowLayoutActivity extends Activity { /** Called when the activity is first created. */ private Button button1; private Button button2; private TextView text1; private TextView text2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub jumpLayout2(); } }); //这里不能定义button2的的时间,否则会出错。但不知道什么原因,难道button2是R.layout.main的控件,才会出现这个情况? /* button2 = (Button)findViewById(R.id.button2); button2.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub jumpLayout1(); } });*/ } public void jumpLayout1() { setContentView(R.layout.main); button1 = (Button) findViewById(R.id.button1); button1.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { jumpLayout2(); } }); } public void jumpLayout2() { setContentView(R.layout.mylayout); button2 = (Button) findViewById(R.id.button2); button2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { jumpLayout1(); } }); } } |