2011-04-04 69 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();