最近,我一直致力于开发一款Android应用,其中Main
活动有5个按钮,所有按钮都指向新活动。
现在我遇到的问题是,当我点击说第二个按钮然后第五个活动打开然后按下后退按钮第4个活动打开然后第3个然后第2个活动。我无法弄清楚问题。
代码:
package com.example.lenovo.meme_generator_new;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainIKActivity extends AppCompatActivity implements
View.OnClickListener {
Button button1,button2,button3,button4,button5,change;
ImageView imageView1,imageView2,imageView3,imageView4,imageView5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_ik);
init();
}
public void init()
{
button1 = (Button) findViewById(R.id.button1);
button2 = (Button) findViewById(R.id.button2);
button3 = (Button) findViewById(R.id.button3);
button4 = (Button) findViewById(R.id.button4);
button5 = (Button) findViewById(R.id.button5);
imageView1 = (ImageView) findViewById(R.id.imageView1);
imageView2 = (ImageView) findViewById(R.id.imageView2);
imageView3 = (ImageView) findViewById(R.id.imageView3);
imageView4 = (ImageView) findViewById(R.id.imageView4);
imageView5 = (ImageView) findViewById(R.id.imageView5);
change= (Button) findViewById(R.id.change);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
button5.setOnClickListener(this);
change.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()) {
case R.id.button1:
Intent intent1 = new Intent(MainIKActivity.this,ik1.class);
startActivity(intent1);
break;
case R.id.button2 :
Intent intent2 = new Intent(MainIKActivity.this,ik2.class);
startActivity(intent2);
case R.id.button3 :
Intent intent3 = new Intent(MainIKActivity.this,ik3.class);
startActivity(intent3);
case R.id.button4 :
Intent intent4 = new Intent(MainIKActivity.this,ik4.class);
startActivity(intent4);
case R.id.button5 :
Intent intent5 = new Intent(MainIKActivity.this,ik5.class);
startActivity(intent5);
break;
case R.id.change :
Intent intentc = new Intent(MainIKActivity.this,MainOMActivity.class);
startActivity(intentc);
}
}
}
答案 0 :(得分:0)
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ThisActivity.this,NextActivity1.class);
startActivity(intent);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ThisActivity.this,NextActivity2.class);
startActivity(intent);
}
});
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ThisActivity.this,NextActivity3.class);
startActivity(intent);
}
});
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ThisActivity.this,NextActivity4.class);
startActivity(intent);
}
});
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ThisActivity.this,NextActivity5.class);
startActivity(intent);
}
});
在每个“下一个活动”中添加此“覆盖”方法
@Override
public void onBackPressed() {
finish();
}
答案 1 :(得分:0)
使用以下代码:
Intent intent = new Intent(this, A.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish(); /
上面的代码将关闭主线程之上的所有其他活动。 并确保关闭所有break语句。