Edittext в Android Alert - PullRequest
       14

Edittext в Android Alert

1 голос
/ 25 марта 2012

Я пытаюсь показать предупреждение с двумя полями edittext в Android. Источник следующий:

public void UserPass(){
    final SharedPreferences prefs=getSharedPreferences("PrefsPreferences",MODE_PRIVATE);
    String user=prefs.getString("user", "");
    String pass=prefs.getString("password", "");

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("U/P");
    builder.setCancelable(false);

    final EditText input1 = new EditText(this);
    final EditText input2 = new EditText(this);
    builder.setView(input1);
    builder.setView(input2);

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            //My code
        }
    })

    .setNegativeButton("No", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            dialog.cancel();
        }
    });
    AlertDialog alert = builder.create();
    alert.show();
}

Отображается только один EditText. Что я делаю не так?

Спасибо!

Ответы [ 2 ]

5 голосов
/ 25 марта 2012
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

public class DialogWithInputBox extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final AlertDialog.Builder alert = new AlertDialog.Builder(this);
    final EditText input = new EditText(this);
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            String value = input.getText().toString().trim();
            Toast.makeText(getApplicationContext(), value,
                    Toast.LENGTH_SHORT).show();
        }
    });

    alert.setNegativeButton("Cancel",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    dialog.cancel();
                }
            });
    alert.show();

}

}

1 голос
/ 25 марта 2012

Второй setView вызов заменяет EditText, установленный в первом. Вы не можете добавить два View таким образом. Вместо этого создайте LinearLayout, добавьте к этому оба значения input1 и input2, а затем добавьте его к builder.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...