简体   繁体   中英

Button onclick and edittext

    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) {


            }



        });

Hey guys my code includes a for loop such that it generates a row inclusive of a textview and 2 edit text. How ever, i do not know what to put in the in the btn2 onClick method as i am having a problem trying to uniquely identify each edit text. For example i am unable to extract a particular input by the user if any of those edit text were to change.

you can get text from both editexts and compare to text that you've setted before.

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

       }

Set IDs for the TextView and EditText when adding the row using textView.setId(int id);

You'll then be able to reference them using the same id.

TextView textView = (TextView) findViewById(id);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM