【问题标题】:How to add TextView and EditText using default AlertDialog programmatically如何以编程方式使用默认 AlertDialog 添加 TextView 和 EditText
【发布时间】:2013-09-02 04:00:07
【问题描述】:

我一直在尝试在默认的 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,其中包含由alertDialogBuilder.setMessage("Input Student ID"); 设置的消息,但我想将此设置为TextView,以便我可以居中对齐它。我该怎么做?

【问题讨论】:

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


    【解决方案1】:
            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();
            }
    

    【讨论】:

    • 这可行,但是我希望 tv1 位于 EditText 之上,所以我将 layout.setOrientation(LinearLayout.HORIZONTAL); 更改为 layout.setOrientation(LinearLayout.VERTICAL);tv1et 都没有显示?
    • 非常感谢!我真的很感激这一点。为已编辑的部分 +1。
    • @Radon8472,很高兴为您提供帮助。
    【解决方案2】:

    制作一个 .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:orientation="vertical">
    
    <TextView
            android:id="@+id/textView1"
            android:layout_width="312dp"
            android:layout_height="wrap_content"
            android:text="Enter your email address :"
            android:layout_marginLeft="5dp"
            android:textAppearance="?android:attr/textAppearanceSmall"/>
    
    <EditText
            android:id="@+id/dialog1Edittext"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:layout_marginTop="10dp"
            android:inputType="textEmailAddress">
    
        <requestFocus/>
    </EditText>
    

    之后在您的 .java 文件中实现以下代码。

    View view = View.inflate(this, R.layout.yourxmlname, null);
    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    // Now set the dialog's content
    alert.setContentView(view);
    

    希望对你有所帮助。

    【讨论】:

    • 谢谢你,但我想使用默认的警报对话框,所以我必须以编程方式添加它。
    【解决方案3】:

    当调用setView() 时,原来的TextView 会隐藏消息。 你需要

    • 创建一个小的布局 xml 并将 EditText 和 TextView 放入其中
    • 扩展布局
    • 使用findViewById() 获取视图的引用
    • 对视图做点什么

    【讨论】:

    • 但是,我无法获得 android 默认警报对话框布局或 Holo Light 主题。
    • 为什么不呢?默认的 AlertDialog 主题设置在外部。您只需传递一个带有孩子的视图而不是普通的 EditText。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 2023-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多