0
我設計了一個alertdialog,當用戶按下保存按鈕時,希望它可以將用戶輸入的editTexts(玩家分數)返回到警報對話框返回到進一步處理。將alertdialog中的EditTexts返回到MainActivity
這怎麼可能呢?會有樣品嗎?非常感謝!!
我執行了以下,但現在輸出的是它不能識別什麼是被輸入,並要求用戶輸入一個非空EDITTEXT(在底部通過其他途徑進入):
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!
你試過了什麼,你遇到了什麼問題? – MAC
非常感謝您的prmopt回覆!我已經添加了上面的代碼,基本上是用戶在alertdialog中輸入5個editTexts,然後點擊保存時,它會繼續到addButtonListener。但是,現在的輸出是無法識別輸入內容,並提醒用戶輸入非空白editText(通過底部的else路由) – pearmak