我正在创建一个动态布局,其中我有2个EditText和一个Button。点击一个按钮,就会创建一个新的同一个布局王。我想,当我点击按钮时,我应该能够从edittexts获取数据并存储它们然后添加新的布局。我无法从edittext获取数据,它是空白的。 以下是我的代码。我还添加了我的布局
的屏幕截图 @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());
}
};
}
答案 0 :(得分:2)
您正在向getOnClickDoSomething
函数传递新创建的editText的文本,因此可以理解它将是空白的。您可能想要的是更改getOnClickDoSomething
,以便它接受对editTexts本身的引用,稍后您可以检索它们的当前文本。 (您必须小心不要泄漏视图,因为您有匿名类持有视图。)
示例:只需更改以下行,如下所示
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());