【问题标题】:Retrieving value and display from dynamically created edit text从动态创建的编辑文本中检索值和显示
【发布时间】:2013-01-12 15:32:31
【问题描述】:

我有一个按钮,当我点击它时,它会在我输入一个值时显示edittext,但是我无法显示输入的值来显示我使用过textview的值,但我无法显示。

class DynamicbuttonActivity extends Activity {

    TextView view= new TextView(getApplicationContext());

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ScrollView scrl=new ScrollView(this);

        final LinearLayout ll=new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        scrl.addView(ll);

        Button add_btn=new Button(this);
        add_btn.setText("Click to add EditTexts");
        ll.addView(add_btn);

        Button add_btn1=new Button(this);
        add_btn1.setText("Click to view TextViiews");
        ll.addView(add_btn1);

        add_btn.setOnClickListener(new OnClickListener() {
            public void onClick(View v)  { 
                EditText et=new EditText(getApplicationContext());
                ll.addView(et);
            }
        });

        add_btn1.setOnClickListener(new OnClickListener()  {    
            String strg;
            public void onClick(View v)  {
                strg = et.getText().toString();
                view.setText(strg);
                ll.addView(view);    
            }
        });

        this.setContentView(scrl);
    }  
}

你能帮帮我吗?

【问题讨论】:

  • TextView view = new TextView(getApplicationContext()); 不好。在OnCreate() 中进行这些初始化。

标签: android


【解决方案1】:

您的实现方式错误。首先单击按钮时,它将添加EditText,但这是您在onclick 方法中定义的,因此您不会进入另一个onclick 方法,其范围仅限于第一个onclick 方法

每次单击第一个按钮时,第二件事都会在您的布局中添加 EditText。我不明白你对这段代码的定义。我的定义是当用户在edittext 上输入文本并单击要在TextView 中显示的值的按钮然后只需删除第一个按钮并在onclick 之外定义您的EditText 方法就像您声明TextView

public class DynamicbuttonActivity extends Activity {

    TextView view= new TextView(getApplicationContext());
    EditText et=new EditText(getApplicationContext());

    public void onCreate(Bundle savedInstanceState){

        super.onCreate(savedInstanceState);

        ScrollView scrl=new ScrollView(this);

        final LinearLayout ll=new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        scrl.addView(ll);

        /*Button add_btn=new Button(this);
        add_btn.setText("Click to add EditTexts");
        ll.addView(add_btn);*/
        Button add_btn1=new Button(this);
        add_btn1.setText("Click to view TextViiews");
        ll.addView(add_btn1);
        /*add_btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v){ 
            }

        });*/

        add_btn1.setOnClickListener(new OnClickListener() { 
            String strg;
            public void onClick(View v){
                strg = et.getText().toString();
                view.setText(strg);
            }

        });
        ll.addView(et);
        ll.addView(view);
        this.setContentView(scrl);

    }

}

【讨论】:

  • 那么我如何显示我在edittext中输入的值
猜你喜欢
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多