如何从Android中动态创建的编辑文本中获取价值?
如何从Android中动态创建的EditText获取值?如何从Android中动态创建的编辑文本中获取价值?
这是我的动态视图(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但它不适合我,问题是当我执行此代码我的添加新编辑文本按钮没有回应我。
这是我想获得插入值,但它只返回最后创建编辑文本的价值:
public Cursor getPerson(String Query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query, null);
return c;
}
某人是StringBuilder sb;
,w是String w;
String quaryWayPoints = "select * from route_table where trip_id = " + t;
Cursor s = myDb.getPerson(quaryWayPoints);
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());
虽然我没有明确你的问题是什么,但我仍会尽力帮助。 这段代码的功能是什么,点击顶部的按钮,将会在运行时添加一个新的布局(例如一个你想要的布局),即带有按钮的edittext,当你点击该按钮时,A toast将显示各个编辑文本的值。因此解决的问题越来越动态添加的EditText
MainActivity
public class MainActivity extends AppCompatActivity {
Button generateET;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
generateET = (Button) findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add, null);
Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);
buttonWayPoints.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (editTextWayPoints.getText().toString() != null) {
Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);
}
});
}
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.techmahindra.stackques.MainActivity">
<Button
android:id="@+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="add editText To layout" />
<ScrollView
android:layout_below="@+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="@+id/dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
</RelativeLayout>
to_add.xml(布局,这将是的值在运行时充气)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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: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>
thax @ankitpurwar –
欢迎@RayVaniya我希望这能整理出你的问题。另外,如果你觉得它有用,请注意upvote。 –
有一些问题与您的代码。您正在尝试将editTextWayPoints添加到mainActivity,但editTextWayPoints已经有一个父视图。我认为你应该将视图添加到mainActivity(mainActivity.addView(view))。最后,要访问editTextWayPoints的值,只需执行editTextWayPoints.getText()。toString();
我能够创建视图但无法获得价值,它返回我上次创建的值编辑文本 –
请参阅我对此问题的回答:http://stackoverflow.com/questions/43527340/gettting-the-value-of -edittext-which-created-programmatically/43527683#43527683 –
你能在这里发布错误日志 –
'editTextWayPoints.getText()。toString()'应该从你的EditText中得到你的文本。 –
请注意,增加喊叫或投票建议往往会吸引downvotes,如果两者兼而有之,则会加倍。请不要添加到您的帖子。 – halfer