我设计了一个alertdialog,当用户按下保存按钮时,希望它可以将警报对话框中用户输入的editTexts(玩家的分数)返回给活动以进行进一步处理。
怎么可以这样做?会有样品吗?非常感谢!!
我已执行以下操作,但现在输出是无法识别正在输入的内容并要求用户输入非空白的editText(通过底部的else路径):
public OnClickListener NewRowButtonListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("Add Scores"); //set title
View view = getLayoutInflater().inflate(R.layout.custom_dialog_add, null);
dialog.setView(view);
dialog.create();
final AlertDialog test = dialog.create();
SlotAlert_add_EditText = (EditText) view.findViewById(R.id.Slot_Alert_add_EditText);
P1Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P1_Alert_add_ScoreEditText);
P2Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P2_Alert_add_ScoreEditText);
P3Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P3_Alert_add_ScoreEditText);
P4Alert_add_ScoreEditText = (EditText) view.findViewById(R.id.P4_Alert_add_ScoreEditText);
Button alert_add_save = (Button) view.findViewById(R.id.Alert_add_SaveButton); //find the Button object within the inflated view
alert_add_save.setOnClickListener(addButtonListener); //set the onClickListener
test.show(); //show the dialog
}
};
然后
public OnClickListener addButtonListener = new OnClickListener()
// create a new Button and add it to the ScrollView
{
private String SlotAppend0EditText;
@Override
public void onClick(View v)
{
SlotEditText.setText(""+SlotAlert_add_EditText); //try to set EditText in MainActivity using the one in the alert dialog above
P1ScoreEditText.setText(""+P1Alert_add_ScoreEditText);
P2ScoreEditText.setText(""+P2Alert_add_ScoreEditText);
P3ScoreEditText.setText(""+P3Alert_add_ScoreEditText);
P4ScoreEditText.setText(""+P4Alert_add_ScoreEditText);
if (SlotEditText.getText().length() <4 &&
SlotEditText.getText().length() > 0 &&
P1ScoreEditText.getText().length() > 0 &&
P2ScoreEditText.getText().length() > 0 &&
P3ScoreEditText.getText().length() > 0 &&
P4ScoreEditText.getText().length() > 0 )
{
SaveScoresToFile(SlotAppend0EditText,
P1ScoreEditText.getText().toString(),
P2ScoreEditText.getText().toString(),
P3ScoreEditText.getText().toString(),
P4ScoreEditText.getText().toString());
} // end if
else (another alert message saying please enter valid input) //NOW THE RESULT IS SHOWING THIS!
答案 0 :(得分:0)
看到这个
Button b = (Button) findViewById(R.id.LeftMenu);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
CreateAlertDialog ();
}
});
警报对话
private void CreateAlertDialog () {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Title");
alert.setMessage("Message");
// Set an EditText view to get user input
final EditText input = new EditText(getActivity());
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
}