机器人|在对话框多个onclicklistener [英] android | multiple onclicklistener in dialog

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

问题描述

在我的活动,我开始一个简单的对话框。

 最后一个对话框myDialog =新的对话框(本);
myDialog.setContentView(R.layout.testing);
...

我testing.xml布局包括什么,但10 ImageViews,id`s是1到10。

我要每个ImageView的是点击,并做一些事情。
该定义.xml文件isn`t工作的onclick()梅索德,正如看到的对话框时,梅索德无法找到。

我得到了它的唯一方法工作如下:定义10的onclick-听众:

  ImageView的img_1 =(ImageView的)myDialog.findViewById(R.id.1);
ImageView的img_2 =(ImageView的)myDialog.findViewById(R.id.2);
...img_1.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
  execute_funtion(1);
  myDialog.cancel();
}
});img_2.setOnClickListener(新OnClickListener(){
@覆盖
公共无效的onClick(查看视图){
  execute_funtion(2);
  myDialog.cancel();
}
});...

不过,这是非常糟糕的code,我有10次几乎相同的线。

所以我的问题:我怎样才能使用干净code的工作?
我想到了一个多onclicklistener(overwride onClick的()函数,并在功能或类似的东西,一个开关/箱),但它不工作。

我很高兴每一个想法!
谢谢

/ EDIT

下面的.xml文件的一个片段

 < ImageView的
  机器人:ID =@ + ID / 1
  机器人:layout_width =WRAP_CONTENT
  机器人:layout_height =WRAP_CONTENT
  机器人:填充=2DP
  安卓的onClick =myFunction的
  机器人:SRC =@绘制/ ic_launcher/>


解决方案

请您的活动实现OnClickListener,然后处理onClick事件象下面这样:

  @覆盖
    公共无效的onClick(视图v){
        开关(v.getId()){
        案例R.id.img1:
            ...
            打破;
        案例R.id.img2:
            ...
            打破;

Inside my Activity I start a simple dialog.

final Dialog myDialog = new Dialog(this);
myDialog.setContentView(R.layout.testing);
...

My testing.xml Layout consists of nothing but 10 ImageViews, id`s are '1' to '10'.

I want every ImageView to be clickable and to do something. The define the onclick() methode in the .xml file isn`t working, as the methode can't be found when the dialog is viewed.

The only way I got it work is following: define 10 onclick-listeners:

ImageView img_1 = (ImageView) myDialog.findViewById(R.id.1);
ImageView img_2 = (ImageView) myDialog.findViewById(R.id.2);
...

img_1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
  execute_funtion(1);
  myDialog.cancel();
}
});

img_2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
  execute_funtion(2);
  myDialog.cancel();
}
});

...

However, that's really bad code, I have 10 times nearly the same lines.

So my question: How can I make that work with clean code? I thought about a multiple onclicklistener (overwride the onClick() function and make a switch/case in the functions or something like that), but it's not working.

I'm happy about every idea! Thanks

/EDIT

Here a snippet of the .xml file

<ImageView
  android:id="@+id/1"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:padding="2dp"
  android:onClick="myFunction"
  android:src="@drawable/ic_launcher" />

解决方案

Make your Activity implements OnClickListener and then process the onClick event like below:

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.img1:
            ...
            break;
        case R.id.img2:
            ...
            break;

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

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