35

入力ダイアログを開くコードは次のとおりです。

final AlertDialog.Builder alert = new AlertDialog.Builder(this);  
alert.setTitle("Dialog Title");  
alert.setMessage("Request information");  
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.edittextautotextlayout, null);
final EditText inputBox = (EditText) textEntryView.findViewById(R.id.my_et_layout);
alert.setView(inputBox);

ソフト キーボードが表示される前にテキスト入力行をタップする必要があることを除いて、これは正常に機能します。

ここで与えられたアドバイスに従って、挿入しようとしました:

inputBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            alert.getWindow().setSoftInputMode( 
               WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

しかし、「メソッド getWindow() はタイプ AlertDialog.Builder に対して定義されていません」という Eclipse オブジェクト。

setOnFocusChangeListener コードは AlertDialog オブジェクトでは機能しますが、AlertDialog.Builder では機能しないようです。ソフト キーボードが自動的に表示されるようにするには、コードをどのように変更すればよいですか。

4

14 に答える 14

83

内部の特定のフォームウィジェットがフォーカスを取得した後ではなく、ダイアログが開いたらすぐにキーボードを表示する必要がある限り(たとえば、ダイアログにEditTextボタンとボタンだけが表示されている場合)、次の操作を実行できます。

AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(
    WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();

.show()ビルダーをすぐに呼び出すのではなく、代わりに呼び出すことができ.create()ます。これにより、ビルダーを画面に表示する前に、追加の処理を行うことができます。

于 2011-05-25T11:44:16.723 に答える
16

miannelle さんへの返信です。

メニュー オプションが選択されると、次のメソッドが呼び出されます。

private void addNote() {
    final Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.textentryalertdialog);
    dialog.setTitle("Add note");
    TextView msgText = (TextView) dialog.findViewById(R.id.messagetext);
    msgText.setText("Whatever prompt you want");
    final EditText inputLine = (EditText) dialog.findViewById(R.id.my_edittext);
    Button okButton = (Button) dialog.findViewById(R.id.OKButton);
    okButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            dialog.dismiss();
            // app specific code
        }           
    });
    Button cancelButton = (Button) dialog.findViewById(R.id.CancelButton);
    cancelButton.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            dialog.dismiss();
        }           
    });
    dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    dialog.show();
}

textentryalertdialog.xml ファイルは、以下を含むリニア レイアウトを定義します。

TextView android:id="@+id/メッセージテキスト" ...

EditText android:id="@+id/my_edittext" ...

ボタン android:id="@+id/OKButton" ...

ボタン android:id="@+id/CancelButton" ...

これが役立つことを願っています。

于 2011-01-28T08:50:56.847 に答える
7

Mur Votema の励まし (上記の彼の回答を参照) で、Dialog クラスに基づいてカスタム ダイアログを作成することで、私の質問に答えました。AlertDialog.Builder に基づくアラートとは異なり、このようなカスタム ダイアログは getWindow().setSoftInputMode(...) コマンドを受け入れるため、ソフト キーボードを自動的に表示できます。

カスタム ダイアログの作成に関するガイダンスについては、この Web ページを見つけましたが、これは特に役に立ちました。

于 2010-11-06T21:05:16.990 に答える
2

EditText -> inputBox.requestFocus() などにフォーカスを設定しようとしましたか?

于 2010-10-30T14:33:13.240 に答える
2

ソフト キー パッドと共にダイアログ ボックスをポップアップ表示して、ユーザーがダイアログ ボックス内の編集テキストをタップしてキーパッドを表示する必要がないようにする場合 (たとえば、ダイアログ ボックスから値を取得する場合)、次の単純なコードを使用します。それはあなたの問題を解決します。

        public void onClick(final View v) 
        {   
             AlertDialog.Builder alert = new AlertDialog.Builder(v.getContext());                 
              alert.setIcon(R.drawable.smsicon);
              alert.setTitle(" Secrete Code");  
              alert.setMessage("Enter a Key for secrete text !");
              final EditText shft_val = new EditText(v.getContext()); 
              shft_val.setInputType(InputType.TYPE_CLASS_NUMBER);//changing the keyBoard to No only,restrict the string etc
              alert.setView(shft_val);

     //pOp Up the key pad on Edit Text  focus event

             shft_val.setOnFocusChangeListener(new OnFocusChangeListener()
             {
                public void onFocusChange(View arg0, boolean arg1)
                {  InputMethodManager inputMgr = (InputMethodManager)v.getContext().
                                    getSystemService(Context.INPUT_METHOD_SERVICE);
                    inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
                        }
                    });

                 alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() 
                 {  
                 public void onClick(DialogInterface dialog, int whichButton) 
                 {
                    //Your specific code... 
                 }
                 });
                 alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                     public void onClick(DialogInterface dialog, int which) {                       
                         dialog.dismiss();
                         return;   
                     }
                 });
                       alert.show();
                    }
于 2012-12-09T18:26:10.287 に答える
2

ビューを使ってみる

v.getWindow().setSoftInputMode( 
           WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
于 2010-10-30T14:20:12.030 に答える
1

使ってみてinputBox

inputBox.getWindow().setSoftInputMode( 
               WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
于 2010-10-29T18:43:25.410 に答える
1

元の質問でほとんど機能していたと思います。final AlertDialog呼び出し先getWindow()を作成してみてください。

// Create the dialog used to modify the mailbox alias
final AlertDialog dialog = alert.create();

inputBox.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            dialog.getWindow().setSoftInputMode( 
               WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }
});

このコードをテストしたところ、次のようなほとんどのデバイスでキーボードが自動的に表示されるようになりました。

  • サムスン ギャラクシー タブ OS 2.2
  • サムスンギャラクシー S OS 2.1
  • HTCセンセーションOS 2.3.4

このソリューションに関するその他のコメント:

1)XMLにフォーカスをリクエストするものがすでにあるかどうかを確認してください。これにより、このコードが機能しなくなる可能性があります(リンク先の質問のTedによると)。

2) このコードは、OS 2.3.4 を実行している HTC G2 では機能しないようです。これは物理キーボードがあるためだと思いますが、WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLEオプションが機能しない可能性がありますか?

SOFT_INPUT_STATE_VISIBLE (ALWAYS なし) も試しましたが、キーボードが自動的に表示されなくなりました。

3) コードを追加して、ユーザーがキーボードの [完了] ボタンを押して変更を送信できるようにすることもできます。

inputAlias.setOnKeyListener(new OnKeyListener()
{
  @Override
  public boolean onKey(View v, int keyCode, KeyEvent event)
  {
    if (keyCode == KeyEvent.KEYCODE_ENTER &&
        inputAlias.isFocused() &&
        inputAlias.getText().length() != 0)
    {
      // Save the new information here

  // Dismiss the dialog
      dialog.dismiss();
      return true;
    }
    return false;
  }
});
于 2011-10-23T18:30:34.180 に答える
1

私は知っています、この質問は本当に古いですが、私は約20の異なるいわゆる「解決策」を試したので、投稿します。

この回答は、私を正しい方向に向けてくれた Pir Fahim Shah の回答に基づいています (ありがとう)。

これをアクティビティの onCreate に入れて、ダイアログが閉じられたときに強制キーボードが非表示になるようにしてください。

this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

次に、次のようなダイアログを作成します。

    AlertDialog.Builder builder = new Builder(this);
    builder.setTitle("title");
    final EditText input = new EditText(this);
    input.setText("text");
    input.setSelection(input.getText().length()); // set cursor to end
    builder.setView(input);
    input.setOnFocusChangeListener(new OnFocusChangeListener()  {
       public void onFocusChange(View v, boolean hasFocus) { 
           if(hasFocus) {
               InputMethodManager inputMgr = (InputMethodManager)v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
               inputMgr.toggleSoftInput(InputMethodManager.SHOW_FORCED,InputMethodManager.HIDE_IMPLICIT_ONLY);
           }
       }
    });
    builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
            // do something here
            dialog.dismiss();
        }
    });
    builder.setNegativeButton("Cancel", null);
    builder.show();
于 2014-12-10T21:45:37.053 に答える
0

LinearLayout次のコードスニペットでは、で任意のをホストする方法を示していDialogFragmentます。1つは使用する方法AlertDialog(ソフトキーボードが機能しない場合)と使用しない方法AlertDialog(ソフトキーボードが機能する場合):

public static LinearLayout createLinearLayout(Context context)
{
    LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.setOrientation(LinearLayout.VERTICAL);
    linearLayout.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    String html;
    html = "<form action=search method=get >\n";
    html += "Google Search: <input name=q value=\"Johnny Depp\" /><br/>\n";
    html += "<input type=submit name=output value=search /><br/>\n";
    html += "</form>\n";
    WebView webView = new WebView(context);
    webView.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient());
    webView.setWebViewClient(new WebViewClient());
    webView.loadDataWithBaseURL("http://www.google.com", html, "text/html", "UTF-8", null);
    linearLayout.addView(webView);
    return linearLayout;
}

public void showWithAlertDialog()
{
    DialogFragment dialogFragment = new DialogFragment()
    {
        public Dialog onCreateDialog(Bundle savedInstanceState)
        {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder
                .setTitle("With AlertDialog")
                .setView(createLinearLayout(getActivity()));
            return builder.create();
        }
    };
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    dialogFragment.show(fragmentTransaction, "dialog");
}

public void showWithoutAlertDialog()
{
    DialogFragment dialogFragment = new DialogFragment()
    {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            getDialog().setTitle("Without AlertDialog");
            getDialog().setCanceledOnTouchOutside(false);
            return createLinearLayout(getActivity());
        }
    };
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    dialogFragment.show(fragmentTransaction, "dialog");
}
于 2012-11-29T23:46:18.957 に答える
0

これは、さまざまな場所から呼び出されたアラートダイアログで機能することがわかりました

        case R.id.birthyear:
        case R.id.ymax:
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            break;

私は他にもたくさんのことを試しました。繊細そうです。

于 2016-02-02T12:54:10.053 に答える