gpt4 book ai didi

java - 获得某些 Intent

转载 作者:太空狗 更新时间:2023-10-29 16:15:04 26 4
gpt4 key购买 nike

假设我有 4 个按钮,每个按钮都包含一个导航到一个 Activity 的 Intent 。它们都导航到一个相同的 Activity 。当我单击第一个按钮时,我希望新 Activity 显示“嗨”,当我单击第二个按钮时,我希望它显示“再见”,依此类推。我怎么做 ?这是一个简单的代码开始

public class Intentt extends Activity {
Button b1,b2,b3,b4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button)findViewById(R.id.button2);
b2 = (Button)findViewById(R.id.button3);
b3 = (Button)findViewById(R.id.button4);
b4 = (Button)findViewById(R.id.button5);
b1.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b2.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b3.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
b4.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intentt.this,MainActivity.class);
startActivity(i);
}
});
}

最佳答案

只需通过 intent extras 或 intent extra Bundle 传递一些数据:

Intent i=new Intent(context,MainActivity.class);
i.putExtra(id, 453);
context.startActivity(i);

例子:

Button b1, b2, b3, b4;
Context context = this;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_intentt);
b1 = (Button) findViewById(R.id.button2);
b2 = (Button) findViewById(R.id.button3);
b3 = (Button) findViewById(R.id.button4);
b4 = (Button) findViewById(R.id.button5);

b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_1);
startActivity(i);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(context, MainActivity.class);
i.putExtra(MainActivity.ID_ACTION, MainActivity.ACTION_2);
startActivity(i);
}
});
}

然后在该 Activity 中,您可以打开该值,使它们成为这样:

String id = intent.getStringExtra("id");
int id = intent.getIntExtra("id",-1);

然后使用if-elseif-elseswitch 语句根据传递的操作更改操作,这样您就可以显示您的消息

要正确接收它们,您需要将 Activity 的启动模式设置为“标准because of this bugAndroidManifest.xml 中创建

public static final int ACTION_1 = 1;
public static final int ACTION_2 = 2;
public static final int ACTION_NULL = -1;
public static final String ID_ACTION = "action_id";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

int id = getIntent().getIntExtra(ID_ACTION, -1);

if (id == ACTION_NULL) {
Log.d("TAG", "id is null");
Toast.makeText(MainActivity.this, "id is null!", Toast.LENGTH_SHORT).show();
} else if (id == ACTION_1) {
Log.i("TAG", "ALLOHA! from button 1");
Toast.makeText(MainActivity.this, "Aloha from button 1!", Toast.LENGTH_LONG).show();
} else if (id == ACTION_2) {
Log.i("TAG", "Hello from button 2");
Toast.makeText(MainActivity.thi,"Hello from button 2!", Toast.LENGTH_LONG).show();
}
}

关于java - 获得某些 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31170411/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com