如何从 Android 中动态创建的编辑文本中获取值?
How to get value from dynamically created edit text in Android?
如何从 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
.[=21= 的原因]
这是我的代码:
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 但它对我不起作用,问题是当我实现此代码时我的 添加新的编辑文本按钮 没有回应我。
这就是我尝试获取插入值的方式,但它 returns 只有最后创建的编辑文本的值:
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 添加到 mainActivity,但 editTextWayPoints 已经有一个父级,即视图。我认为您应该将视图添加到 mainActivity (mainActivity.addView(view))。最后,要访问 editTextWayPoints 的值,只需执行 editTextWayPoints.getText().toString();
尽管我没明白你的问题到底是什么,但我还是会尽力提供帮助。
这段代码的确切作用是,单击顶部的按钮,一个新布局(您想要的布局)将在运行时添加到 activity 屏幕,即带有按钮的编辑文本,当您单击该按钮时,将显示具有相应编辑文本值的吐司。因此解决了获取动态添加的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>
如何从 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
.[=21= 的原因]
这是我的代码:
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 但它对我不起作用,问题是当我实现此代码时我的 添加新的编辑文本按钮 没有回应我。
这就是我尝试获取插入值的方式,但它 returns 只有最后创建的编辑文本的值:
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 添加到 mainActivity,但 editTextWayPoints 已经有一个父级,即视图。我认为您应该将视图添加到 mainActivity (mainActivity.addView(view))。最后,要访问 editTextWayPoints 的值,只需执行 editTextWayPoints.getText().toString();
尽管我没明白你的问题到底是什么,但我还是会尽力提供帮助。 这段代码的确切作用是,单击顶部的按钮,一个新布局(您想要的布局)将在运行时添加到 activity 屏幕,即带有按钮的编辑文本,当您单击该按钮时,将显示具有相应编辑文本值的吐司。因此解决了获取动态添加的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>