【发布时间】:2017-04-27 12:40:24
【问题描述】:
如何从 Android 中动态创建的 EditText 中获取价值?
这是我的动态视图(XML 文件)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@drawable/background"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="@+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
我使用LayoutInflater 是因为我想要完美的设计布局,而这不是我通过setLayoutParams 完成的,所以这就是我使用LayoutInflater 的原因。
这是我的代码:
LayoutInflater l = getLayoutInflater();
final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
mainActivity.addView(editTextWayPoints);
我是 Android 新手。大多数人建议this solution 但它对我不起作用,问题是当我实现此代码时,我的 ADD NEW EDIT TEXT BUTTON 没有回应我。
这是我试图获取插入值的方式,但它只返回最后创建的编辑文本的值:
public Cursor getPerson(String Query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query, null);
return c;
}
sb 是StringBuilder sb;,w 是String w;
String queryWayPoints = "select * from route_table where trip_id = " + t;
Cursor s = myDb.getPerson(queryWayPoints);
int count3 = s.getCount();
for (int j = 0; j < count3; j++) {
s.moveToNext();
w = s.getString(s.getColumnIndex("waypoints"));
// se.append(w + "." + "00");
}
trip_name.setText(n);
trip_date.setText(d);
sb.append(w);
}
trip_route.setText(sb.toString());
【问题讨论】:
-
你能把错误日志贴在这里
-
editTextWayPoints.getText().toString()应该从您的 EditText 中获取文本。 -
请注意,添加大喊大叫或投票建议往往会吸引反对票,如果两者都这样做,则会加倍。请不要在您的帖子中添加任何一个。
标签: android dynamic android-edittext layout-inflater