6

私の場合: フォーカスが無効になっている EditText フィールドが 1 つあります。EditText フィールドの横に、入力メソッド用の 2 つのボタンがあります。したがって、最初のボタンをクリックしたときに、ソフトキーボードを開いて EditText フィールドのテキストを編集する必要があります。私は多くの方法を試します:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

そして私にはうまくいきません。ソフト キーボードを開く唯一の方法は次のとおりです。

toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)

しかし、EditText フィールドから情報を編集する方法はありません。

ボタンをクリックしたときにキーボードを開いて EditText のテキストを編集する方法を教えてください。どうもありがとう!

編集:

そのため、EditText はデフォルトではフォーカス可能ではありません。[キーボード] ボタンをクリックすると、フォーカス可能である必要があり、テキストを入力して EditText に表示するソフト キーボードが表示されます。挿入する他の方法は、キーボードを必要としない ABC ボタンです。それはモールス符号入力のようなものになります - ABC ボタンを長押しします :) 私の場合に実装する提案された例を試してみます。君たちありがとう :)

私の場合

4

7 に答える 7

22

助けてくれてありがとう:)私はあなたがくれたすべての提案を使用し、他の多くのスクリプトを検索してテストし、最終的に私のコードは機能しています:)

これが私の最終的なコードです:

InputEditText = (EditText) findViewById(R.id.InputText);

public void InputData() {

        /* Keyboard Button Action */
        KeyboardButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Log.v(TAG, "On Keyboard Button click event!");

                InputEditText.requestFocus();
                InputEditText.setFocusableInTouchMode(true);

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(InputEditText, InputMethodManager.SHOW_FORCED);

            }

        });

}

誰かに役立つかもしれません:)ありがとう!

于 2013-02-12T13:53:33.323 に答える
13

あなたが説明したデザインは推奨されません。focusableユーザーがコンポーネント内のテキストを変更できるかどうかを制御することではない属性の目的に違反していEditTextます。

ユースケースで必要と思われるために別の入力方法を提供する予定がある場合 (たとえば、編集可能なテキスト フィールドで特定の記号のセットのみを許可するなど)、ユーザーが許可されていない間はテキスト編集を完全に無効にする必要があります。フィールドの値を変更します。

編集可能なフィールドを宣言します。

<EditText
    android:id="@+id/edit_text_2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10" />

そのfocusable属性がデフォルト値のままになっていることに注意してください。わかりました、後で処理します。編集プロセスを開始するボタンを宣言します。

<Button
    android:id="@+id/button_show_ime"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start editing" />

今、あなたのActivity宣言で:

private EditText editText2;
private KeyListener originalKeyListener;
private Button buttonShowIme;

そして、これをonCreate()行います:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ime_activity);

    // Find out our editable field.
    editText2 = (EditText)findViewById(R.id.edit_text_2);
    // Save its key listener which makes it editable.
    originalKeyListener = editText2.getKeyListener();
    // Set it to null - this will make the field non-editable
    editText2.setKeyListener(null);

    // Find the button which will start editing process.
    buttonShowIme = (Button)findViewById(R.id.button_show_ime);
    // Attach an on-click listener.
    buttonShowIme.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // Restore key listener - this will make the field editable again.
            editText2.setKeyListener(originalKeyListener);
            // Focus the field.
            editText2.requestFocus();
            // Show soft keyboard for the user to enter the value.
            InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(editText2, InputMethodManager.SHOW_IMPLICIT);
        }
    });

    // We also want to disable editing when the user exits the field.
    // This will make the button the only non-programmatic way of editing it.
    editText2.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // If it loses focus...
            if (!hasFocus) {
                // Hide soft keyboard.
                InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(editText2.getWindowToken(), 0);
                // Make it non-editable again.
                editText2.setKeyListener(null);
            }
        }
    });
}

すべてのコメントを含むコードが一目瞭然であることを願っています。API 8 および 17 でテスト済み。

于 2013-02-11T18:09:08.120 に答える
5

これを試して :

        final EditText myedit2 = (EditText) findViewById(R.id.myEditText2);

        Button btsmall = (Button) findViewById(R.id.BtSmall);
        btsmall.setOnClickListener(new OnClickListener() {              
            @Override
            public void onClick(View arg0) {
                myedit2.requestFocus();
            }
        });
于 2013-02-11T16:20:40.963 に答える
1
LinearLayout ll_about_me =(LinearLayout) view.findViewById(R.id.ll_about_me);
    ll_about_me.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub

            mEtEmpAboutYou.requestFocus();
            mEtEmpAboutYou.setFocusableInTouchMode(true);

            InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(mEtEmpAboutYou, InputMethodManager.SHOW_FORCED);

            return true;
        }
    });
于 2016-04-27T10:34:57.957 に答える
1

ボタンをクリックしたときにキーボードを開くために、このコードを実行しました。お気に入り 。

btn1 = (Button) findViewById(R.id.btn1);
edt1 = (EditText) findViewById(R.id.edt1);
btn1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            edt1.requestFocus();
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.showSoftInput(edt1, InputMethodManager.SHOW_IMPLICIT);
        }
    });

その完成作品。

于 2015-08-01T16:30:21.350 に答える