質問

Does Android come with a dialog that I can pop up that shows a single input text field with an OK and Cancel button or do I have to create that myself?

役に立ちましたか?

解決

No, there is no such component available but we can manage to achieve this by using setView(View) without creating any xml in layout/inflate. As per the code below:

AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message :");

// Set an EditText view to get user input
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();
        Log.d("", "Pin Value : " + value);
        return;
    }
});

alert.setNegativeButton("Cancel",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // TODO Auto-generated method stub
            return;
        }
});
alert.show();
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top