2011-04-04 97 views
0

我在我的應用程序中有一個項目,點擊它並彈出一個帶有文本框和按鈕的警告對話框。這有點笨重,文本框沒有集中,所以你必須點擊它來調出鍵盤。有沒有更好的方法來提示輸入文字?提示輸入文字

+0

重複此問題:http://stackoverflow.com/q/4054662/131066 – 2011-04-04 14:45:13

回答

1

如果你需要讓它始終打開,你可以使用

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 

然後用editText.requestFocus讓文成有。

或者在EditText佈局上使用android:inputMethod並添加「alwaysVisible」標誌。

0

您可以在AlertDialog上的EditText上創建焦點偵聽器,然後獲取AlertDialog的窗口。從那裏你可以通過調用setSoftInputMode來進行軟鍵盤顯示。

final AlertDialog dialog = ...; 

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
    @Override 
    public void onFocusChange(View v, boolean hasFocus) { 
     if (hasFocus) { 
      dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
     } 
    } 
}); 
0
AlertDialog.Builder builder = new AlertDialog.Builder(tab3.this); 
        builder.setTitle(params[4]); 
        final EditText et = new EditText(tab3.this); 
        et.setText(params[5]); 

        builder.setView(et); 
        builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { 
         @Override 
         public void onClick(DialogInterface dialog, int which) { 
          params[5] = et.getText().toString(); 
          ViewGroup vg = (ViewGroup) arg1; 
          TextView txv = (TextView)vg.findViewById(R.id.datetext_view2); 
          txv.setText(params[5]); 
          form_adapter.datas.put(ckey, params); 
          save(GlobalVars.current_id,form_adapter.datas); 

         } 
        }); 

       final AlertDialog alert = builder.create(); 
       et.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
         @Override 
         public void onFocusChange(View v, boolean hasFocus) { 
          if (hasFocus) { 
           alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
          } 
         } 
        }); 
       alert.show();