机器人 - 多OnClickListener? [英] Android - multiple OnClickListener?

查看:171
本文介绍了机器人 - 多OnClickListener?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有4个图像。我们应该能够点击这些图像。 我想知道,如果我要创建4 OnClickListener ,或者还有另一种方式来做到这正常吗?

 公共类NavigateActivity扩展活动实现OnClickListener {

    //图片
    私人ImageView的电话;
    私人ImageView的预订;
    私人ImageView的设置;
    私人ImageView的图片;

    @覆盖
    保护无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.navigate);
        手机=(ImageView的)findViewById(R.navigate.callcenter);
        phone.setOnClickListener(新OnClickListener(){
            公共无效的onClick(视图v){
                如果(V ==手机){
                    AlertDialog alertDialog =新AlertDialog.Builder(NavigateActivity.this).create();
                    alertDialog.setTitle(注意);
                    alertDialog.setMessage(Etes-VOUS河畔德vouloir appeler乐呼叫中心);

                    alertDialog.setButton(OUI,新DialogInterface.OnClickListener(){
                       公共无效的onClick(DialogInterface对话,诠释它){
                          意图callIntent =新的意图(Intent.ACTION_CALL);
                            callIntent.setData(Uri.parse(电话:1232456789));
                            startActivity(callIntent);
                       }
                    });

                    alertDialog.setButton2(非,新DialogInterface.OnClickListener(){
                           公共无效的onClick(DialogInterface对话,诠释它){
                               dialog.cancel();
                           }
                        });
                    alertDialog.show();
                }
            }
        });
 }

    @覆盖
    公共无效的onClick(视图v){
        // TODO自动生成方法存根

    }
}
 

解决方案

您只需像这样,

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

和在onclick()方法,

  @覆盖
    公共无效的onClick(视图v){

       如果(V ==手机){
              //你的东西
        }
       否则,如果(V ==预订){
              //你的东西
        }
       否则,如果(V ==设置){
              //你的东西
        }
       如果ESE(V ==图片){
              //你的东西
        }
    }
 

I have 4 images. We should be able to click on these images. I'd like to know if I have to create 4 OnClickListener, or there is another way to do this properly?

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

    }
}

解决方案

You can just do it like this,

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

And in the onClick() method,

 @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
        }
    }

这篇关于机器人 - 多OnClickListener?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆