Java 从edittext实例视图获取数据
我使用自定义布局创建了一个自定义的多个EditText字段。现在的问题是,当用户单击时,不知道如何从这些EditText获取数据。这是按钮Java 从edittext实例视图获取数据,java,android,android-edittext,Java,Android,Android Edittext,我使用自定义布局创建了一个自定义的多个EditText字段。现在的问题是,当用户单击时,不知道如何从这些EditText获取数据。这是按钮 b.setId(MY_BUTTON); b.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Toast toast; Log.w(TAG, "View Id: " + v.
b.setId(MY_BUTTON);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast;
Log.w(TAG, "View Id: " + v.getId());
toast = Toast.makeText(getApplicationContext(),"Cliquer",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25,400);
toast.show();
saveForm();
final Map<String, String> params = new HashMap<String, String>();
LinearLayout root = (LinearLayout) findViewById(R.id.linearLayout1);
View tempView;
String assembledString = "";
for (int i = 0; i < root.getChildCount(); i++) {
tempView = root.getChildAt(i);
if (tempView instanceof EditText) {
assembledString += ((EditText) tempView)
.getText().toString();
params.put(name,assembledString);
Log.d(TAG,"Data EditText : "+ assembledString);
}
/*
* EditText et = (EditText)
* ((ViewGroup
* )v.getParent()).getChildAt
* (i);
*/
}
savePost(action, params);
}
});
ll.addView(b);
b.setId(我的按钮);
b、 setOnClickListener(新视图。OnClickListener(){
公共void onClick(视图v){
吐司;
Log.w(标记,“视图Id:+v.getId());
toast=toast.makeText(getApplicationContext(),“Cliquer”,
吐司长度(长);
toast.setGravity(Gravity.TOP,25400);
toast.show();
saveForm();
final Map params=new HashMap();
LinearLayout根=(LinearLayout)findViewById(R.id.linearLayout1);
视图tempView;
字符串assembledString=“”;
for(int i=0;i
我解决了我的问题
这就是解决方案
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast;
Log.w(TAG, "View Id: " + v.getId());
toast = Toast.makeText(getApplicationContext(),"Cliquer",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25,400);
toast.show();
LinearLayout container = (LinearLayout) findViewById(R.id.linearLayout2);
ViewGroup parent = container ;
String[] array = new String[parent.getChildCount()];
String[] arrayFields = new String[parent.getChildCount()];
Map<String, String> params = new HashMap<String, String>();
for (int i=0; i < parent.getChildCount(); i++){
View child = parent.getChildAt(i);
if (child instanceof EditText)
{
EditText et = (EditText) child;
array[i] = et.getText().toString();
arrayFields[i] = et.getTag().toString();
params.put( arrayFields[i], array[i]);
Log.d(TAG , "array : " + array[i]);
Log.d(TAG , "arrayFields: " + arrayFields[i]);
Log.d(TAG , "paramsString : " + params);
}
else { Log.d(TAG , "Mes autres Views : " + child );}
}
savePost(action, params);
}
});
ll.addView(b);
b.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
吐司;
Log.w(标记,“视图Id:+v.getId());
toast=toast.makeText(getApplicationContext(),“Cliquer”,
吐司长度(长);
toast.setGravity(Gravity.TOP,25400);
toast.show();
LinearLayout容器=(LinearLayout)findViewById(R.id.linearLayout2);
视图组父对象=容器;
String[]数组=新字符串[parent.getChildCount()];
String[]arrayFields=新字符串[parent.getChildCount()];
Map params=新的HashMap();
对于(int i=0;i
只需更改以下行
final Map params=new HashMap()代码>
与
Map<String, String> params = new HashMap<String, String>();
Map params=newhashmap();
您的问题只能通过标记来解决。
使用editText.setTag(position)在我显示时保存位置,或者保存对象Id(无论您喜欢什么)
例如:
List<EditText> editTextList = new ArrayList<>();
for(EditText et : editTextList){
String tempString = et.getText().toString();
params.put(name, tempString);
}
List editTextList=newArrayList();
用于(EditText et:editTextList){
String tempString=et.getText().toString();
参数put(名称、临时字符串);
}
现在你得到了你想要的。:)
//我就是这样解决的
您还可以使用列表保存编辑文本
首先,在创建后将EditText添加到列表中
第二,使用editText.setTag(position)在我显示时保存位置,或者保存对象Id(无论您喜欢什么)
点击按钮,你们可以从编辑文本列表中获取数据。您还可以从EditText的标记中知道位置或id
希望这对你有帮助。这就是我解决问题的方法
您的示例代码
List<EditText> editTextList = new ArrayList<>();
b.setId(MY_BUTTON);
b.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast toast;
Log.w(TAG, "View Id: " + v.getId());
toast = Toast.makeText(getApplicationContext(),"Cliquer",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 25,400);
toast.show();
saveForm();
final Map<String, String> params = new HashMap<String, String>();
for(EditText et : editTextList){
assembledString = et.getText().toString();
params.put(name, assembledString);
}
savePost(action, params);
}
});
ll.addView(b);
List editTextList=newArrayList();
b、 setId(MY_U按钮);
b、 setOnClickListener(新视图。OnClickListener(){
公共void onClick(视图v){
吐司;
Log.w(标记,“视图Id:+v.getId());
toast=toast.makeText(getApplicationContext(),“Cliquer”,
吐司长度(长);
toast.setGravity(Gravity.TOP,25400);
toast.show();
saveForm();
final Map params=new HashMap();
用于(EditText et:editTextList){
assembledString=et.getText().toString();
参数put(名称,汇编字符串);
}
savePost(操作、参数);
}
});
ll.addView(b);
Final变量是导致此问题的原因自定义多个EditText是什么意思?如果(EditText的tempView实例){assembledString+=((EditText)tempView).getText().toString();params.put(名称,assembledString),我这里有个问题;Log.d(标记,“数据编辑文本:”+assembledString);}谢谢兄弟,