【问题标题】:Button onclick and edittext按钮 onclick 和 edittext
【发布时间】:2014-06-28 12:24:01
【问题描述】:
    for(int x = 1; x < i ; x++){
        // VIEWS
         TextView tvx = new TextView(this);
         EditText etx = new EditText(this);
         EditText editx = new EditText(this);
         // ADD VIEW
        //set properties

        ll1.addView(tvx);
        ll1.addView(etx);
        ll1.addView(editx);
            // sets the ingredient  and the amount and cal corresponding

        switch(x){

        case 1: tvx.setText("Chicken");
                etx.setText("200");
                editx.setText("100g");

            break;
        case 2:tvx.setText("eggs");
                etx.setText("350");
                editx.setText("100g");
            break;
        case 3:tvx.setText("Bacon");
                etx.setText("400");
                editx.setText("100g");

            break;
        case 4:
            tvx.setText("Salad");
            etx.setText("200");
            editx.setText("100g");

        }



        //  // // // // // // // 


        btn2.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {


            }



        });

大家好,我的代码包含一个 for 循环,以便生成包含 textview 和 2 的行 编辑文本。但是,我不知道在 btn2 onClick 方法中放入什么,因为我在尝试唯一标识每个编辑文本时遇到问题。 例如,如果这些编辑文本中的任何一个要更改,我将无法提取用户的特定输入。

【问题讨论】:

    标签: java android android-layout android-edittext android-button


    【解决方案1】:

    您可以从两个编辑文本中获取文本并与您之前设置的文本进行比较。

         @Override
         public void onClick(View arg0) {
            if (etx.getText.toString().matches("400")){
               //some code here
            } else if (etx.getText.toString().matches("200")){
                //some code here
            } 
    
           }
    

    【讨论】:

    • 这确实不是最好的方法。
    【解决方案2】:

    使用textView.setId(int id);添加行时为 TextView 和 EditText 设置 ID

    然后您就可以使用相同的 ID 引用它们。

    TextView textView = (TextView) findViewById(id);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 2012-08-28
      • 2021-11-02
      • 2014-08-10
      • 1970-01-01
      相关资源
      最近更新 更多