Get text from dynamically created EditText on clic

2019-06-06 09:48发布

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 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条回答
走好不送
2楼-- · 2019-06-06 10:15

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()); 
查看更多
登录 后发表回答