如何确定从动态生成的表点击按钮 [英] How to identify the button clicked from a dynamically generated table

查看:124
本文介绍了如何确定从动态生成的表点击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我动态从表中的字符串array.Each行填充表也有一个加号和减号按钮递增/递减一列的值。这些按钮也像动态下code创建。在这里,我怎么能在单击检测准确按钮。即;如果我点击第二行的+按钮,我怎么能得到按钮的ID点击进行进一步的处理。

  plusButton =新按钮(本);
 minusButton =新按钮(本);
 CreateView的(TR,TV1,名称由[i]);
 CreateView的(TR,TV2,(字符串)(地名第[i + 1]));
 minusButton.setId(ⅰ);
 minusButton.setText( - );
 minusButton.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
 plusButton.setId(ⅰ);
 plusButton.setText(+);
 plusButton.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));`


解决方案

您可以为每个按钮的 onClickListener 监听器。从 view.getId()方法使用该按钮的ID在你的的onClick()方法来识别按钮点击

您可以添加单独的侦听器为每个按钮喜欢这里(假设您正在设置为每个按钮ID对应一个行)

  minusButton.setOnClickListener(新View.OnClickListener(){
        公共无效的onClick(视图v){
             //做越来越v.getId后减去一些操作()来获取当前行
        }
    }
);

编辑:

我假设你的code是这个样子。纠正我,如果存在偏差。

 按钮minusButton = NULL;
的for(int i = 0; I< rowCount时,我++)
{
    minusButton =新按钮(本);
    minusButton.setId(ⅰ);
    //设置其他的东西,并添加到布局
    minusButton.setOnClickListener(本);
}

让你的类实现接口 View.OnClickListener 和实施的onClick()方法。

 公共无效的onClick(视图v){
    //如果一个加号按钮或减号按钮上的文字可以告诉你
    //按钮BTN =(按钮)V;
    //如果(BTN){btn.getText();}
    //的getId()应该告诉你的行号
    // v.getId()
}

I am populating a table dynamically from a string array.Each row of the table also has a plus and minus button to increment/decrement the value of one column. These buttons are also dynamically created like in the code below. Here how can I detect the exact button upon clicking. i.e; if I click on the '+' button of the 2nd row, how can I get the id of the button clicked for further processing.

 plusButton= new Button(this);
 minusButton= new Button(this);
 createView(tr, tv1, names[i]);
 createView(tr, tv2, (String)(names[i+1]));
 minusButton.setId(i);
 minusButton.setText("-");
 minusButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
 plusButton.setId(i);
 plusButton.setText("+");
 plusButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));`

解决方案

You can set an onClickListener listener for each button. Use the id of the button from view.getId() method on your onClick() method to identify the button click.

You can add separate listeners for each button like here (assuming that the id you are setting for each button corresponds to a row)

minusButton.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
             // Do some operation for minus after getting v.getId() to get the current row
        }
    }
);

Edit:

I am assuming your code is like this. Correct me if there is a deviation.

Button minusButton = null;
for(int i = 0; i < rowCount; i++)
{
    minusButton = new Button(this);
    minusButton.setId(i);
    // set other stuff and add to layout
    minusButton.setOnClickListener(this);
}

Let your class implement the interface View.OnClickListener and implement the onClick() method.

public void onClick(View v){
    // the text could tell you if its a plus button or minus button
    // Button btn = (Button) v;
    // if(btn){ btn.getText();}
    // getId() should tell you the row number
    // v.getId()
}

这篇关于如何确定从动态生成的表点击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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