Get text from dynamically created EditText on click of a button

安稳与你 提交于 2019-12-23 03:45:15

问题


I am creating a dynamic layout in which I have 2 EditText and one Button. On click of a button a new same king of layout is created. I want , when I click on the button I should be able to get data from edittexts and store them and then add new layout.I am not able to get data from edittext , it is coming up blank. Below is my code . I am also adding screenshot of my layout

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        ll_newView = (LinearLayout)findViewById(R.id.ll_newView);
        scrollView = (ScrollView)findViewById(R.id.scrollView);
        bt_addView = (Button)findViewById(R.id.bt_addView);




        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
        LinearLayout one = new LinearLayout(MainActivity2.this);


        //=====First EditText======//
        params.weight = 5.0f;
        params.setMargins(16,8,8,16);
        EditText  et_one = new EditText(MainActivity2.this);
        et_one.setPadding(10,10,10,10);
        et_one.setHint("Edit Text 1");
        et_one.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
        et_one.setLayoutParams(params);
        one.addView(et_one);

        //=====Second EditText======//
        EditText  et_two = new EditText(MainActivity2.this);
        et_two.setPadding(10,10,10,10);
        et_two.setHint("Edit Text 2");
        et_two.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
        et_two.setLayoutParams(params);
        one.addView(et_two);


        Button bt_plus = new Button(MainActivity2.this);
        bt_plus.setText("+");


        one.addView(bt_plus);


        ll_newView.addView(one);

        bt_plus.setOnClickListener(getOnClickDoSomething(et_one.getText().toString().trim(),et_two.getText().toString().trim()));


    }

    View.OnClickListener getOnClickDoSomething(final String one1,final String two1)  {
        return new View.OnClickListener() {
            public void onClick(View v) {
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);

                LinearLayout one = new LinearLayout(MainActivity2.this);
                Log.e("========","=====one ="+one1+"=========two = "+two1);
                //=====First EditText======//
                params.weight = 5.0f;
                params.setMargins(16,8,8,16);
                EditText  et_one = new EditText(MainActivity2.this);
                et_one.setPadding(10,10,10,10);
                et_one.setHint("Edit Text 1");
                et_one.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
                et_one.setLayoutParams(params);
                one.addView(et_one);

                //=====Second EditText======//
                EditText  et_two = new EditText(MainActivity2.this);
                et_two.setPadding(10,10,10,10);
                et_two.setHint("Edit Text 2");
                et_two.setBackground(getResources().getDrawable(R.drawable.edit_text_border));
                et_two.setLayoutParams(params);
                one.addView(et_two);


                Button bt_plus = new Button(MainActivity2.this);
                bt_plus.setText("+");

                one.addView(bt_plus);



                ll_newView.addView(one);

                bt_plus.setOnClickListener(getOnClickDoSomething(et_one.getText().toString().trim(),et_two.getText().toString().trim()));
                Log.e("========","=====one ="+et_one.getText().toString().trim()+"=========two = "+ et_two.getText().toString().trim());

            }
        };
    }

回答1:


You are passing to the getOnClickDoSomething function the text of the newly created editText, so understandably it will be blank. What you may want is to change getOnClickDoSomething, so that it accepts reference to the editTexts themselves, and later you can retrieve the their current text. (You will have to be careful not to leak views, as you have anonymous classes holding on to the Views.)
Example: Just change the following lines as follows

bt_plus.setOnClickListener(getOnClickDoSomething(et_one, et_two)); }


View.OnClickListener getOnClickDoSomething(final EditText one1,final EditText two1) 


Log.e("========","=====one ="+one1.getText().toString().trim(),+"=========two = "+two1.getText().toString().trim()); 


来源:https://stackoverflow.com/questions/46108600/get-text-from-dynamically-created-edittext-on-click-of-a-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!