单击方法中的动态按钮和getTag

时间:2017-06-25 16:59:50

标签: java android

我正在尝试创建动态按钮并在其中添加点击功能。我尝试在按钮数组的元素中使用getTag方法。每当我尝试运行这些代码应用程序崩溃我尝试了两个,即v.getTag()和button.getTag()。谢谢你的帮助。

public class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.testing);
        //test creating of dynamic buttons
         Button[] myButton = new Button[4];
        LinearLayout scrViewButLay = (LinearLayout) findViewById(R.id.layout);
        for (int index = 0; index < 4; index++) {
            myButton[index] = new Button(this); //initialize the button here
            myButton[index].setText("Button # ");
            myButton[index].setTag(index);
            scrViewButLay.addView(myButton[index]);
          myButton[index].setOnClickListener(getOnClickDoSomething(myButton[index]));
        }
    }


  View.OnClickListener getOnClickDoSomething(final Button button)  {
        return new View.OnClickListener() {
            public void onClick(View v) {
                button.setText("text now set.. ");
                //Log.i("info",(String) button.getTag());
                Log.i("info",(String) v.getTag());
            }

        }; 
   }
}

LogCat错误--------------------------------------------- ----------------------

com.example.mandeep.pos1_1, PID: 26112
                                                                            java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
                                                                                at com.example.mandeep.pos1_1.MainActivity$1.onClick(MainActivity.java:43)
                                                                                at android.view.View.performClick(View.java:4757)
                                                                                at android.view.View$PerformClick.run(View.java:19757)
                                                                                at android.os.Handler.handleCallback(Handler.java:739)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                at android.os.Looper.loop(Looper.java:135)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5219)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)

1 个答案:

答案 0 :(得分:0)

尝试使用它:

Log.i("info", ""+v.getTag());  

或者如果要将其解析为int。

final int tag = Integer.parseInt(""+v.getTag());