将纯文本输入放在AlertDialog中

时间:2015-06-22 14:06:43

标签: android android-alertdialog

如何在AlertDialog中输入纯文本输入?

public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Enter The Subject Name");
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getActivity(), "Canceled", Toast.LENGTH_SHORT).show();
        }
    });
    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getActivity(), "Subject Added", Toast.LENGTH_SHORT).show();
        }
    });
    Dialog dialog = builder.create();
    return dialog;
}

2 个答案:

答案 0 :(得分:1)

您正在寻找的是AlertDialog.Builder的setView方法。您可以创建包含edittext的xml布局,然后将其设置为对话框的视图:

<EditText
    android:id="@+id/myEditText"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="@dimen/activity_horizontal_margin"
    android:layout_marginRight="@dimen/activity_horizontal_margin"/>

然后你可以使用

final View customLayout = getActivity().getLayoutInflater().inflate(R.layout.edittext, null);
builder.setView(customLayout);

Dialog dialog = builder.create();
EditText editText = (EditText) customLayout.findViewById(R.id.myEditText);
editText.setText("Editable text");

答案 1 :(得分:0)

exitByBackKey(); // call in main class

//Custom Dialog Java file
public void exitByBackKey() {

        tableDialog = new Dialog(DashBoardActivity.this);
        tableDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        tableDialog.setContentView(R.layout.exit_dialog);
        tableDialog.setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        Window window = tableDialog.getWindow();
        lp.copyFrom(window.getAttributes());
        lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        tableDialog.getWindow().setGravity(Gravity.CENTER);
        tableDialog.setCancelable(true);
        tableDialog.show();
    enter code here
        CustomTextView txtMessage = (CustomTextView) tableDialog
                .findViewById(R.id.txtMessage);
        `enter code here`CustomButton btnDialogOk = (CustomButton) tableDialog
                .findViewById(R.id.btnAlertOk);
        CustomButton btnDialogcancel = (CustomButton) tableDialog
                .findViewById(R.id.btnAlertCancel);
        txtMessage.setText("Do you want to stop this Application?");
        btnDialogOk.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tableDialog.dismiss();
                DashBoardActivity.this.finish();
            }
        });
        btnDialogcancel.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                tableDialog.dismiss();

            }
        });
    }

//Xml File of custom Dialog  exit_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/header_bg"
    android:orientation="vertical"
    android:padding="10dp" >

    <innovify.hustl.library.CustomTextView
        android:id="@+id/txtMessage"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="@android:color/white" />

    <LinearLayout
        android:id="@+id/ABC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" >

        <innovify.hustl.library.CustomButton
            android:id="@+id/btnAlertOk"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="150dp"
            android:layout_height="48dp"
            android:layout_gravity="center"
            android:layout_marginRight="5dp"
            android:layout_marginTop="10dp"
            android:background="#e2e2e2"
            android:gravity="center"
            android:text="Ok" />

        <innovify.hustl.library.CustomButton
            android:id="@+id/btnAlertCancel"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_width="150dp"
            android:layout_height="48dp"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:background="#e2e2e2"
            android:gravity="center"
            android:text="Cancel" />
    </LinearLayout>

</LinearLayout>