馬上加入Android 台灣中文網,立即免費下載應用遊戲。
您需要 登錄 才可以下載或查看,沒有帳號?註冊
x
本帖最後由 i88i632u 於 2016-4-11 13:47 編輯
最近在做學校的專題
我用imageButton1連到下一層layout
但是想要用手機上的返回鍵時卻會跳出程式
只能用第二層的Button才能返回
第一層的java
package com.example.text3;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton button = (ImageButton)findViewById(R.id.imageButton1);
button.setOnClickListener(new ImageButton.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(MainActivity.this, TestExam002.class);
startActivity(intent);
MainActivity.this.finish();
}
});
第二層的java
package com.example.text3;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public class TestExam002 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
Button button = (Button)findViewById(R.id.Button02);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setClass(TestExam002.this, MainActivity.class);
startActivity(intent);
TestExam002.this.finish();
}
}); |

|