【问题标题】:AlertDialog EditText setHint() dosent show upAlertDialog EditText setHint() 不显示
【发布时间】:2016-01-13 16:29:29
【问题描述】:

我正在尝试将 setHint() 添加到 AlertDialog 但它不会显示?

AlertDialog.Builder alertDialog = new AlertDialog.Builder(TestActivityForDialog.this);

    // Setting Dialog Title
    alertDialog.setTitle("Search By Name");

    // Setting Dialog Message
    alertDialog.setMessage("Please enter name:");

    // Setting Icon to Dialog
    alertDialog.setIcon(R.mipmap.ic_launcher);

    //set editText ----
    final EditText input = new EditText(this);
    input.setHint("Last Name (4 - 10 Chars)");
    input.setHintTextColor(Color.YELLOW);
    alertDialog.setView(input);

【问题讨论】:

    标签: android


    【解决方案1】:

    您忘记将 EditText 与对话框链接,如下所示

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(TestActivityForDialog.this);
    
    // Setting Dialog Title
    alertDialog.setTitle("Search By Name");
    
    // Setting Dialog Message
    alertDialog.setMessage("Please enter name:");
    
    // Setting Icon to Dialog
    alertDialog.setIcon(R.mipmap.ic_launcher);
    
    EditText input = (EditText) dialog.findViewById(R.id.editbox);  //THIS GUY
    input.setHint("Last Name (4 - 10 Chars)");
    input.setHintTextColor(Color.YELLOW);
    alertDialog.setView(input);
    

    另请阅读:How to give the hint of edittextbox of dialog which is created by code in android


    编辑:根据How to add TextView and EditText using default AlertDialog programmatically,我使用上面的参数创建了一个自定义 AlertDialog。

    这是一个代码:

    @覆盖 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

        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);
    
        //EditText
        EditText input = new EditText(this);
        input.setHint("Last Name (4 - 10 Chars)");
        input.setHintTextColor(Color.YELLOW);
    
        layout.addView(input, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT));
    
        alertDialogBuilder.setView(layout);
        alertDialogBuilder.setTitle("Search By Name");
        alertDialogBuilder.setMessage("Please enter name:");
        alertDialogBuilder.setIcon(R.mipmap.ic_launcher);
        alertDialogBuilder.setCancelable(false);
    
        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();
        }
    
    }
    

    希望对你有帮助

    【讨论】:

    • AlertDialog 在不使用 xml(我宁愿不使用)的情况下工作正常,只是在添加提示代码时它忽略了我。还是谢谢你!
    • 我添加了新的解决方案,同时检查两个链接 ;-)
    • 我在 android studio 中注释掉了我的代码并放入你的,但它仍然没有显示提示。再次感谢您!
    • 我添加了屏幕截图,对我来说它看起来不错 - 您是在设备上尝试过还是仅在布局预览中尝试过?
    • 我在一个设备上试了一下,它没有,不知道为什么。还是谢谢你!
    猜你喜欢
    • 2020-09-20
    • 2017-09-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-10
    • 2015-09-05
    • 2022-07-27
    • 1970-01-01
    • 2011-04-06
    相关资源
    最近更新 更多