「Android Java」backボタンを押してEditTextフォーカスを閉じます


Backボタンを押してEditTextフォーカスをオフにします


前回位置決めで目ボタンを押した時にパスワードが表示されましたが、問題があります.
Enterkeyがクリックしたとき、setOnKey Listenerを使用してキーボードを置いたりフォーカスを消したりするプロセスを行いました.
ただし、上記条件にはbackボタン押下時の条件が追加されているが、操作ができないという問題があり、今回の位置決めでは、この問題を記述する.
  • 解析前動作gif
  • 1.setOnKey Listener backボタン条件の追加


    setOnKeyListenerにbackボタン押下時の条件を追加
    etx1.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (keyCode == KeyEvent.KEYCODE_ENTER || keyCode == KeyEvent.KEYCODE_ENTER) {
                            etx1.clearFocus();
                            InputMethodManager imm =
                                    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(etx1.getWindowToken(), 0);
                            return true;
                        }
                    }
                    return false;
                }
            });
    これでsetOnKeyListener内で条件を修正しても動作していないことを確認できます
    理由は何ですか.
    なぜsetOnKeyListenerに条件を追加しても動作を処理できないのですか?
    その理由は、dextextのポーリング操作がある場合、この方法をどのように解決するかということです.

    2.新しいEditTextクラスの定義


    なぜ
  • setOnKeyListenerに条件
  • が適用されないのか
    上記の問題を解決するために、新しいedtextクラスを定義する必要があります.
    なぜなら、Set On KissnerはonKeyPreImeメソッドを再定義する場合にテキストを適用し、backボタンアクションを追加しても実行しない
  • MbEditText.java
  • import android.content.Context;
    import android.util.AttributeSet;
    import android.view.KeyEvent;
    
    public class MbEditText extends androidx.appcompat.widget.AppCompatEditText {
        private static final String TAG = "실행된것?";
    
        public MbEditText(Context context ) {
            super( context );
        }
        public MbEditText(Context context, AttributeSet attrs ) {
            super( context, attrs );
        }
        public boolean onKeyPreIme( int keyCode, KeyEvent event ) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    this.clearFocus();
                    // Focus_Helper.releaseFocus (etx_email_input);
                }
            }
            return super.onKeyPreIme( keyCode, event );
        }
    
    }
    すでにCustomにDeptテキストが作成されている以上、このコンポーネントをxmlファイルに適用し、main ActivityからこのCustomコンポーネントを取得します.

    解決後のコード

  • text_input_layout.xml
  • <?xml version="1.0" encoding="utf-8"?>
    <ScrollView xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:overScrollMode="never"
        android:orientation="vertical"
        android:focusable="true"
        android:focusableInTouchMode="true">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <FrameLayout
                android:id="@+id/passwordr_case"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintTop_toBottomOf="@+id/password_case">
    
                <com.example.velog.MbEditText
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="textPassword"
                    android:maxLines="1"
                    android:hint="비밀번호를 입력 비밀번호 입력"
                    android:textSize="18dp"/>
    
                <ImageButton
                    android:id="@+id/viewPassword"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_gravity="right|center_vertical"
                    android:layout_marginRight="5dp"
                    android:layout_marginBottom="5dp"
                    android:background="@android:color/transparent"
                    android:scaleType="centerInside"
                    android:src="@drawable/visibleeye"/>
            </FrameLayout>
    
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/android_picture"/>
        </LinearLayout>
    </ScrollView>
    
  • main.java
  • import android.content.Context;
    import android.graphics.Typeface;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.KeyEvent;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.EditText;
    import android.widget.ImageButton;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AppCompatActivity;
    
    import org.w3c.dom.Text;
    
    public class main extends AppCompatActivity {
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.text_input_layout);
    
            final MbEditText etx1 = findViewById(R.id.password);
            Typeface typeFace = Typeface.createFromAsset(getAssets(), "nanumsquare.ttf");
            etx1.setTypeface(typeFace);
            final ImageButton see_pw = findViewById(R.id.viewPassword);
    
            etx1.setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
                    if (event.getAction() == KeyEvent.ACTION_DOWN) {
                        if (keyCode == KeyEvent.KEYCODE_ENTER) {
                            etx1.clearFocus();
                            InputMethodManager imm =
                                    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                            imm.hideSoftInputFromWindow(etx1.getWindowToken(), 0);
                            return true;
                        }
                    }
                    return false;
                }
            });
    
            see_pw.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent motionEvent) {
                    // 터치 동작에 따라 텍스트 인풋 타입 변경
                    switch (motionEvent.getAction()){
                        case MotionEvent.ACTION_DOWN:
                            etx1.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); // 보이는 비밀번호
                            // 위에서 inpuType을 변경하게 되면 커서가 맨 앞으로 오는 문제가 발생하므로
                            // 전체 택스트의 길이만큼 커서를 뒤로 이동 시킨다
                            etx1.setSelection(etx1.length());
                            etx1.setTypeface(typeFace);
                            see_pw.setImageResource(R.drawable.invisibleeye);
                            break;
                        case MotionEvent.ACTION_UP:
                            etx1.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD); // 암호인 텍스트
                            etx1.setSelection(etx1.length());
                            etx1.setTypeface(typeFace);
                            see_pw.setImageResource(R.drawable.visibleeye);
                            break;
                        case MotionEvent.ACTION_CANCEL:
                            etx1.setInputType(InputType.TYPE_CLASS_TEXT|InputType.TYPE_TEXT_VARIATION_PASSWORD); // 암호인 텍스트
                            etx1.setSelection(etx1.length());
                            etx1.setTypeface(typeFace);
                            see_pw.setImageResource(R.drawable.visibleeye);
                            break;
                    }
                    return true;
                }
            });
        }
    }
    以上のコードを適用すると、動作が正しいことを確認できます
  • 完了時動作gif
  • 終わりの時。


    私はサイオンキリスナで反キー動作を追加しましたが、動作が不行き届きで慌てましたが、この問題を解決することで、グーグルで情報を検索した後、サイオンキリス方法はアウンキリスナが先に戻ることを知りました.
    もし私がこれから欲しい動作を実行していないなら、部品の方法の動作順序を確認します.