문제

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