从每个动态创建的EditText中获取数据



通过Add按钮动态创建EditText,我还使用setId((为它们分配了唯一的ID。现在我想做的是在用户点击按钮时从动态创建的EditText中获取值,然后将所有值存储为SharedReference这是代码谢谢你们

添加代码

List<EditText> allEds = new ArrayList<EditText>();
Button btnSave, btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout layoutmain = (LinearLayout) findViewById(R.id.layoutmain);
btnAdd = (Button) findViewById(R.id.btnadd);
btnSave = (Button) findViewById(R.id.btnsave);
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
EditText ed = new EditText(ActivityOptions.this);
ed.setId(ed.generateViewId());
ed.setTag(allEds.size());
ed.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
layoutmain.addView(ed);
// Add also to the allEds
allEds.add(ed);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
EditText ed = new EditText(ActivityOptions.this);
@Override
public void onClick(View arg0) {
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putString("key" + ed.getTag().toString(), ed.getText().toString());

for (EditText ed : allEds) {
editor.putString("key" + ed.getTag().toString(), ed.getText().toString());
}
editor.commit();
}
});
Intent intent = new Intent(this, MainActivity.class);
ArrayList<String> allTexts = new ArrayList<>();
for (EditText e : allEds) {
allTexts.add(e.getText().toString());
}
intent.putExtra("Text", (Serializable) allTexts);
}

}

在创建每个新的EditText时,您可以通过setId()生成id,但我看不出这些在代码中会有什么用处
相反,将EditText的标签设置为:

ed.setTag(allEds.size());

并将其添加到列表中:

allEds.add(ed);

现在,在每个EditText的标签中,您都将其序号(从零开始(存储在列表中,当您想将其值存储在SharedPreferences中时,可以执行以下操作:

SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
editor.putString("key" + et.getTag().toString(), ed.getText().toString());
editor.commit(); 

或者通过使用列表中所有项目的循环:

for (EditText ed : allEds) {
editor.putString("key" + et.getTag().toString(), ed.getText().toString());
}
editor.commit();

通过这种方式,您可以使用以下键存储EditText的所有文本值:

"key0", "key1", "key2",... 

编辑
如果您想打开另一个活动并将列表传递给它:

Intent intent = new Intent(this, AnotherActivity.class);
ArrayList<String> allTexts = new ArrayList<>();
for (EditText e : allEds) {
allTexts.add(e.getText().toString())
}
intent.putExtra("Text", (Serializable) allTexts);

并且在另一个活动的onCreate():中

ArrayList<String> list = (ArrayList<String>) getIntent().getSerializableExtra("Texts");

Edit2
用以下代码替换btnSave.setOnClickListener()中的代码:

btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences.Editor editor = getPreferences(Context.MODE_PRIVATE).edit();
for (EditText ed : allEds) {
editor.putString("key" + ed.getTag().toString(), ed.getText().toString());
}
editor.commit();
Intent intent = new Intent(this, MainActivity.class);
ArrayList<String> allTexts = new ArrayList<>();
for (EditText e : allEds) {
allTexts.add(e.getText().toString());
}
intent.putExtra("Text", (Serializable) allTexts);
startActivity(intent);
}
});  

您忘记了EditText添加到您的列表中,因此保存按钮将了解它们并获取它们的输入。

之后,您需要指定保存按钮(我相信您已经指定了,但我在代码中看不到它的引用(。

最后,只需在其onClick()中实现保存按钮的逻辑(保存在SharedPreferences中(。

List<EditText> allEds = new ArrayList<EditText>();
Button btnSave,btnAdd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout layoutmain = (LinearLayout)findViewById(R.id.layoutmain);
btnAdd = (Button) findViewById(R.id.btnadd);
btnSave = (Button) findViewById(R.id.btnsave);
btnadd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
EditText ed = new EditText(MainActivity.this);
ed.setId(ed.generateViewId());
ed.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
layoutmain.addView(ed);
// Add also to the allEds
allEds.add(ed);
}
});
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
for (EditText ed : allEds) {
editor.putString("saved_text_key", ed.getText().toString());
editor.commit();
}
}
});
}

最新更新