私は2つのEditTextと1つのボタンを持つ動的レイアウトを作成しています。ボタンをクリックすると、同じレイアウトの新しい王が作成されます。ボタンをクリックすると、編集テキストからデータを取得して保存し、新しいレイアウトを追加することができるはずです。私はedittextからデータを取得できません。空白になります。 以下は私のコードです。私はまた、あなたはとても当然、それは空白になり、機能getOnClickDoSomething
に新しく作成されたのEditTextのテキストを渡しているボタンをクリックすると動的に作成された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());
}
};
}
を次のようにただ、次の行を変更するには、あなたが動的に作成する方法をあなたのコードを投稿することができます2つのedittextとボタン、私も同じパターンを作成したい。 –
私はすでに自分のコードを追加しています。作成時にデフォルトでレイアウトを1つ作成してから、ボタンをクリックして他のレイアウトを作成しています。 –
私はそれを得た。あなたは@パラースワット –