我想知道如何获取按钮的值,然后在另一个活动上使用它来相应地显示内容.
例:
我的MainActivity上有3个按钮Image1,Image2和Image3.
现在,根据用户点击的按钮(Image1,Image2和Image3),在新活动上显示相应的图像.
我知道如何创建这些按钮,新活动以及如何在新活动上显示图像.如何根据用户点击的按钮显示图像?
你的类应该实现OnClickListener并覆盖onClick()
Button button1 = (Button) findViewById(R.id.button1);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
button1.setOnClickListener(new OnClickListener(this));
button2.setOnClickListener(new OnClickListener(this));
button3.setOnClickListener(new OnClickListener(this));
@Override
public void onClick(View v){
switch(v.getId()) //get the id which is an int
{
case R.id.button1 : //if its button1 that is clicked
Intent i= new Intent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
// use intents to pass information to secondActivity and display the image there
break;
case R.id.button2 :
Intent i= new Intent("com.example.secondActivity");
startActivity(i)
//use intents to pass information to secondActivity and display the image there
break;
case R.id.button3 :
Intent i= new Intent("com.example.secondActivityy");
startActivity(i)
//use intents to pass information to secondActivity and display the image there
break;
}
}
Run Code Online (Sandbox Code Playgroud)
使用意图传递值
单击按钮
Intent i= new Intent("com.example.secondActivity");
i.puExtra("key",value);
startActivity(i);
Run Code Online (Sandbox Code Playgroud)
在第二个活动中检索它如下
Bundle extras = getIntent().getExtras();
if(extras!=null)
{
int values= extras.getInt("key");
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2090 次 |
| 最近记录: |