请教退出与返回问题,谢谢
我做了个程序,在主界面设置6个按钮,为什么在主界面点击某一按钮进入一个二级界面时,点击二级界面中的“返回”按钮,并不是返回主界面,而是直接退出整个程序?程序代码:
package st.st.st; //主界面 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.widget.ImageButton; import android.widget.Toast; public class St1Activity extends Activity { // 定义一个变量,来标识是否退出 private static boolean isExit = false; Handler mHandler = new Handler() { @Override public void handleMessage(Message msg) { super.handleMessage(msg); isExit = false; } }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initButton(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { exit(); return false; } return super.onKeyDown(keyCode, event); } private void exit() { if (!isExit) { isExit = true; Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show(); // 利用handler延迟发送更改状态信息 mHandler.sendEmptyMessageDelayed(0, 2000); } else { finish(); System.exit(0); } } private void initButton() { // TODO Auto-generated method stub ImageButton btn1=(ImageButton)findViewById(R.id.imageButton1); ImageButton btn2=(ImageButton)findViewById(R.id.imageButton2); ImageButton btn3=(ImageButton)findViewById(R.id.imageButton3); ImageButton btn4=(ImageButton)findViewById(R.id.imageButton4); ImageButton btn5=(ImageButton)findViewById(R.id.imageButton5); ImageButton btn6=(ImageButton)findViewById(R.id.imageButton6); //肥胖测试按钮 btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); //设置Intent对象要启动的Activity intent.setClass(St1Activity.this, St2.class); //通过Intent对象启动另外一个Activity startActivity(intent); St1Activity.this.finish(); } }); //体重记录按钮 btn2.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); //设置Intent对象要启动的Activity intent.setClass(St1Activity.this, St3.class); //通过Intent对象启动另外一个Activity startActivity(intent); St1Activity.this.finish(); } }); //饮食记录按钮 btn3.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); //设置Intent对象要启动的Activity intent.setClass(St1Activity.this, St4.class); //通过Intent对象启动另外一个Activity startActivity(intent); St1Activity.this.finish(); } }); //运动记录按钮 btn4.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); //设置Intent对象要启动的Activity intent.setClass(St1Activity.this, St5.class); //通过Intent对象启动另外一个Activity startActivity(intent); St1Activity.this.finish(); } }); //加油提醒按钮 btn5.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); //设置Intent对象要启动的Activity intent.setClass(St1Activity.this, St6.class); //通过Intent对象启动另外一个Activity startActivity(intent); St1Activity.this.finish(); } }); //退出按钮 btn6.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub finish(); System.exit(0); } }); } }
程序代码:
package st.st.st; //二级界面 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.Editable; import android.text.TextWatcher; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Toast; public class St2 extends Activity { //肥胖测试 public RadioGroup group; public RadioButton man,woman; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.a); Button btn1=(Button)findViewById(R.id.button1);//返回主界面按钮 Button btn2=(Button)findViewById(R.id.button2);//计算按钮 //返回主界面按钮 btn1.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub // Intent intent = new Intent(); //设置Intent对象要启动的Activity // intent.setClass(St2.this,St1Activity.class); //通过Intent对象启动另外一个Activity // startActivity(intent); St2.this.finish(); } }); //计算按钮事件 btn2.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub //取得输入的身高 EditText height=(EditText)findViewById(R.id.height); float height1=0; if(!height.getText().toString().equals("")){ if(!height.getText().toString().substring(0,1).equals(".")){ height1=Float.parseFloat(height.getText().toString()); } } /*取得选择的性别*/ String sex=""; group=(RadioGroup)findViewById(R.id.radioGroup); man=(RadioButton)findViewById(R.id.radio0); woman=(RadioButton)findViewById(R.id.radio1); if(man.isChecked()){ woman.setChecked(false); sex="M"; } else{ woman.setChecked(true); sex="F"; } //判断身高是否为空 if(height1<100.0||height1>200.0||height.equals(null)){ height.requestFocus(); height.setError("取值范围100.0~200.0"); } else if(height1>=100.0||height1>=200.0){ Intent intent=new Intent(); intent.setClass(St2.this, St2_1.class); Bundle bundle=new Bundle(); bundle.putDouble("height", height1); bundle.putString("sex", sex); /*将Bundle对象assign给Intent*/ intent.putExtras(bundle); /*调用St2_1*/ startActivity(intent); } } }); } }