我在将用户的输入数据传递到Android中的警报对话框时遇到问题。
我得到了一个要求用户输入的课程,它会为每个输入分配一个ID。像这样,
public void inputRow( TableLayout tl, String label, int inputSize, int inputID )
{
TableRow inputRow = new TableRow(this);
TextView tv = new TextView(this);
EditText edit = new EditText(this);
// some margin
inputRow.setPadding( 20,10,20,0);
tv.setText(label);
edit.setMaxWidth( inputSize*7) ;
edit.setMinimumWidth(inputSize*7);
edit.setId( inputID );
edit.setGravity(Gravity.RIGHT);
inputRow.addView(tv);
inputRow.addView(edit);
tl.addView(inputRow);
}
然后输入行将类似于:
inputRow(myTableLayout, "Name", 30, 10000);
inputRow(myTableLayout, "Email", 20, 10001);
然后会创建一个警告对话框,以便在单击按钮时显示消息:
final AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Your Inputs");
alertDialog.setMessage("Inputs");
但是当我尝试使用指定的ID加载.setMessage()
时,它将返回null。我得到了一个使用函数View .getID()
的提示,但我仍然是java的新手并且无法使用此函数。提前谢谢。
答案 0 :(得分:0)
您需要引用要为对话框添加的每个编辑文本
您可以通过以下方式执行此操作:
OR
然后,使用该引用,您可以使用getText()。toString()获取值 - 请参阅http://developer.android.com/reference/android/widget/EditText.html#getText()
答案 1 :(得分:0)
如果您拥有EditText
的ID,您应该可以这样做:
//if you do not have a reference to the EditText already -- this uses the IDs in your question
EditText nameEditText = (EditText) myTableLayout.findViewById(10000);
EditText emailEditText = (EditText) myTableLayout.findViewById(10001);
String nameValue = nameEditText.getText().toString();
String emailValue = emailEditText.getText.toString();
// now you can do whatever you need to with the values
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Your Inputs")
.setMessage("Name is: " + nameValue + ", Email is: " + emailValue)
.create();
dialog.show();
使用更多代码进行更新
使用inputRow()
函数
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
inputRow(tableLayout, "Name", 30, 10000);
inputRow(tableLayout, "Email", 20, 10001);
Button doneButton = (Button) findViewById(R.id.done_btn);
doneButton.setOnClickListener(new View.OnClickListener() {
@Override public void onClick(View v) {
//if you do not have a reference to the EditText already -- this uses the IDs in your question
EditText nameEditText = (EditText) tableLayout.findViewById(10000);
EditText emailEditText = (EditText) tableLayout.findViewById(10001);
String nameValue = nameEditText.getText().toString();
String emailValue = emailEditText.getText().toString();
// now you can do whatever you need to with the values
AlertDialog dialog = new AlertDialog.Builder(MainActivity.this)
.setTitle("Your Inputs")
.setMessage("Name is: " + nameValue + ", Email is: " + emailValue)
.create();
dialog.show();
}
});
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TableLayout
android:id="@+id/table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/done_btn"
android:text="done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>