我正在尝试将一个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);
答案 0 :(得分:0)
您忘了将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();
}
}
希望有所帮助