单击按钮即可从动态创建的EditText获取文本 [英] Get text from dynamically created EditText on click of a button

查看:93
本文介绍了单击按钮即可从动态创建的EditText获取文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个动态布局,其中有2个EditText和一个Button.单击按钮后,将创建一个新的相同布局国王.我想要,当我单击按钮时,我应该能够从edittexts中获取数据并存储它们,然后添加新的布局.我无法从edittext中获取数据,它变成空白. 下面是我的代码.我还添加了我的布局的屏幕截图

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());

            }
        };
    }

推荐答案

您要将新创建的editText的文本传递给getOnClickDoSomething函数,因此可以理解为空白.您可能需要更改getOnClickDoSomething,以便它接受对editTexts本身的引用,以后您可以检索其当前文本. (您将要小心,不要泄漏视图,因为您拥有保留在视图上的匿名类.)
示例:只需更改以下几行

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()); 

这篇关于单击按钮即可从动态创建的EditText获取文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆