android - 如何以编程方式使用默认的 AlertDialog 添加 TextView 和 EditText

标签 android android-edittext textview android-alertdialog android-dialog

我一直在尝试在默认的 AlertDialog 中添加两个元素,但我似乎无法让它工作。这是我的代码:

// START Dialog
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

    TextView tv = new TextView(this);
    tv.setText(title);
    tv.setPadding(40, 40, 40, 40);
    tv.setGravity(Gravity.CENTER);
    tv.setTextSize(20);

    EditText et = new EditText(this);
    etStr = et.getText().toString();

    alertDialogBuilder.setView(et);
    alertDialogBuilder.setTitle(title);
    alertDialogBuilder.setMessage("Input Student ID");
    alertDialogBuilder.setCustomTitle(tv);

    if (isError)
        alertDialogBuilder.setIcon(R.drawable.icon_warning);
    // alertDialogBuilder.setMessage(message);
    alertDialogBuilder.setCancelable(false);

    // Setting Negative "Cancel" Button
    alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.cancel();
        }
    });

    // Setting Positive "Yes" Button
    alertDialogBuilder.setPositiveButton("OK",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    if (isError)
                        finish();
                    else {
                        Intent intent = new Intent(
                                ChangeDeviceActivity.this,
                                MyPageActivity.class);
                        startActivity(intent);
                    }
                }
            });

    AlertDialog alertDialog = alertDialogBuilder.create();

    try {
        alertDialog.show();
    } catch (Exception e) {
        // WindowManager$BadTokenException will be caught and the app would
        // not display the 'Force Close' message
        e.printStackTrace();
    }

现在,这只是一个 EditText,带有一条由 alertDialogBu​​ilder.setMessage("Input Student ID"); 设置的消息,但我想把它变成一个 TextView 这样我就可以将它居中对齐。我该怎么做?

最佳答案

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);

        LinearLayout layout = new LinearLayout(this);
        LinearLayout.LayoutParams parms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setLayoutParams(parms);

        layout.setGravity(Gravity.CLIP_VERTICAL);
        layout.setPadding(2, 2, 2, 2);

        TextView tv = new TextView(this);
        tv.setText("Text View title");
        tv.setPadding(40, 40, 40, 40);
        tv.setGravity(Gravity.CENTER);
        tv.setTextSize(20);

        EditText et = new EditText(this);
        etStr = et.getText().toString();
        TextView tv1 = new TextView(this);
        tv1.setText("Input Student ID");

        LinearLayout.LayoutParams tv1Params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        tv1Params.bottomMargin = 5;
        layout.addView(tv1,tv1Params);
        layout.addView(et, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

        alertDialogBuilder.setView(layout);
        alertDialogBuilder.setTitle(title);
        // alertDialogBuilder.setMessage("Input Student ID");
        alertDialogBuilder.setCustomTitle(tv);

        if (isError)
            alertDialogBuilder.setIcon(R.drawable.icon_warning);
        // alertDialogBuilder.setMessage(message);
        alertDialogBuilder.setCancelable(false);

        // Setting Negative "Cancel" Button
        alertDialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        });

        // Setting Positive "OK" Button
        alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                if (isError)
                    finish();
                else {
                      Intent intent = new Intent(ChangeDeviceActivity.this,
                      MyPageActivity.class); startActivity(intent);
                }
            }
        });

        AlertDialog alertDialog = alertDialogBuilder.create();

        try {
            alertDialog.show();
        } catch (Exception e) {
            // WindowManager$BadTokenException will be caught and the app would
            // not display the 'Force Close' message
            e.printStackTrace();
        }

关于android - 如何以编程方式使用默认的 AlertDialog 添加 TextView 和 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18565482/

相关文章:

android - 多行edittext用json android输入字符

android在textview中制作上标文本被削减

android - 带有图标的自定义 TextView

java - 将自定义 header 添加到 WebView 资源请求 - android

找不到安卓资源

java - 在多项 Activity 中使用改造响应的最佳方式

android - 如何让 Android 中的 TextView 具有水平滚动和自动滚动?

android - 即使在我使用了 START_STICKY 之后,服务也不总是运行

android - 单击按钮时如何在android中获取复选框,带有ListView值的EditText?

android - 如何在用户按下回车键时触发操作?