2016-01-12 4 views
0

Я пытаюсь добавить setHint() в AlertDialog, но он не появится?AlertDialog EditText setHint() dosent показать

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

Вы забыли связать 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


EDIT: По How to add TextView and EditText using default AlertDialog programmatically бы я создал пользовательский AlertDialog с помощью параметров выше.

Вот код:

@Override защищен недействительным 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(); 
    } 

} 

enter image description hereenter image description here

Надеется, что это поможет

+0

AlertDialog отлично работает без использования XML (что я лучше не использовать) только при добавлении коды подсказки он проигнорировал меня. В любом случае, спасибо! – ML13

+0

Я добавил новое решение, проверьте также оба ссылки ;-) – piotrek1543

+0

Я прокомментировал свой код в студии android и добавил его, но он все еще не отобразил подсказку. Еще раз спасибо! – ML13

Смежные вопросы