gpt4 book ai didi

Android - 多个 OnClickListener?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:04 28 4
gpt4 key购买 nike

我有 4 张图片。我们应该能够点击这些图像。我想知道我是否必须创建 4 个 OnClickListener,或者是否有其他方法可以正确执行此操作?

public class NavigateActivity extends Activity  implements OnClickListener {

// images
private ImageView phone;
private ImageView bookings;
private ImageView settings;
private ImageView pictures;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.navigate);
phone = (ImageView) findViewById(R.navigate.callcenter);
phone.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (v == phone) {
AlertDialog alertDialog = new AlertDialog.Builder(NavigateActivity.this).create();
alertDialog.setTitle("Attention");
alertDialog.setMessage("Etes-vous sur de vouloir appeler le Call center");

alertDialog.setButton("Oui", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:1232456789"));
startActivity(callIntent);
}
});

alertDialog.setButton2("Non", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
}
}
});
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}
}

最佳答案

你可以这样做,

phone.setOnClickListener(this);
bookings.setOnClickListener(this);
settings.setOnClickListener(this);
pictures.setOnClickListener(this);

然后在 onClick() 方法中,

 @Override
public void onClick(View v) {

if(v == phone){
// your stuff
}
else if(v == bookings){
// your stuff
}
else if(v == settings){
// your stuff
}
ese if(v == pictures){
// your stuff
}
}

关于Android - 多个 OnClickListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8444972/

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