2013-10-18 22 views
16

enter image description hereアンドロイドの次の編集テキストにフォーカスを変更するにはどうすればいいですか?

ユーザは編集テキストに1桁の数字しか入力できません。彼がedtText1の値を入力すると、カーソルが自動的にedtText2に移動するなどします。ユーザは、既に入力したテキストを編集することができる。私は次のようにしました。

edtPasscode1.setOnKeyListener(new OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // TODO Auto-generated method stub 
      if (edtPasscode1.getText().length() == 1) 
       edtPasscode2.requestFocus(); 
      return false; 
     } 
    }); 

    edtPasscode2.setOnKeyListener(new OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // TODO Auto-generated method stub 
      if (edtPasscode2.getText().length() == 1) 
       edtPasscode3.requestFocus(); 
      return false; 
     } 
    }); 

    edtPasscode3.setOnKeyListener(new OnKeyListener() { 

     @Override 
     public boolean onKey(View v, int keyCode, KeyEvent event) { 
      // TODO Auto-generated method stub 
      if (edtPasscode3.getText().length() == 1) 
       edtPasscode4.requestFocus(); 
      return false; 
     } 
    }); 

ユーザーがテキストを編集した場合、カーソルは他の編集テキストに移動し、必要に応じて動作しません。どのように私は上記を達成することができますか?あなたのパスワードを変更したい場合は

+0

適切なフォーカスを得るには、すべてのビューを単一の親にする必要があります。レイアウトの親を確認してください。 –

+0

@Brontokはい、それは単一の親にあります。それらのすべては、水平に配置された線形レイアウトになっています。 –

+0

編集可能な複製の[アンドロイドでのテキストのフォーカス](0120-338-331) –

答えて

27

TextWatcherが編集

...取り扱わにあなたに多くの方法を与えるような場合には、TextWatcher代わりのonKeyListener

B'cozをお試しください: -

StringBuilder sb=new StringBuilder(); 

     edtPasscode1.addTextChangedListener(new TextWatcher() { 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 
       // TODO Auto-generated method stub 
       if(sb.length()==0&edtPasscode1.length()==1) 
       { 
        sb.append(s); 
        edtPasscode1.clearFocus(); 
        edtPasscode2.requestFocus(); 
        edtPasscode2.setCursorVisible(true); 

       } 
      } 

      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

       if(sb.length()==1) 
       { 

        sb.deleteCharAt(0); 

       } 

      } 

      public void afterTextChanged(Editable s) { 
       if(sb.length()==0) 
       { 

        edtPasscode1.requestFocus(); 
       } 

      } 
     }); 

希望の作品です。

+0

edtText1を編集したい場合はどうなりますか? –

+0

@RethinavelVelu私の更新された答えをチェックしてください。あなたのパスワードを編集することもできます。 –

+0

@ Amit Gupta。ありがとうボス。私は朝からこれを探していた。それはとてもうまくいく。あなたの答えを受け入れてうれしいです。この質問に答えることに感謝します。 –

2

android:maxLength="1"としてeditetxt、以下のコード

((EditText) findViewById(R.id.edi1)).addTextChangedListener(new TextWatcher() { 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
      if(((EditText) findViewById(R.id.edi1)).getText().toString().length()==1) 
      { 
       ((EditText) findViewById(R.id.edi1)).clearFocus(); 
       ((EditText) findViewById(R.id.edi2)).requestFocus(); 
       ((EditText) findViewById(R.id.edi2)).setCursorVisible(true); 

      } 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 


    }); 

    ((EditText) findViewById(R.id.edi2)).addTextChangedListener(new TextWatcher() { 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
      if(((EditText) findViewById(R.id.edi2)).getText().toString().length()==1) 
      { 
       ((EditText) findViewById(R.id.edi2)).clearFocus(); 
       ((EditText) findViewById(R.id.edi3)).requestFocus(); 
       ((EditText) findViewById(R.id.edi3)).setCursorVisible(true); 

      } 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 


    }); 


    ((EditText) findViewById(R.id.edi3)).addTextChangedListener(new TextWatcher() { 
     public void onTextChanged(CharSequence s, int start, int before, int count) { 
      // TODO Auto-generated method stub 
      if(((EditText) findViewById(R.id.edi3)).getText().toString().length()==1) 
      { 
       ((EditText) findViewById(R.id.edi3)).clearFocus(); 
       ((EditText) findViewById(R.id.edi4)).requestFocus(); 
       ((EditText) findViewById(R.id.edi4)).setCursorVisible(true); 

      } 
     } 

     public void beforeTextChanged(CharSequence s, int start, int count, 
       int after) { 

      // TODO Auto-generated method stub 

     } 

     @Override 
     public void afterTextChanged(Editable s) { 
      // TODO Auto-generated method stub 

     } 


    }); 
4

に従うように長さを設定します。これは

に動作するはずです以下のコード

edtxt1 = (EditText) findViewById(R.id.edtxt_phonenumber_one); 
     edtxt2 = (EditText) findViewById(R.id.edtxt_phonenumber_two); 
     edtxt3 = (EditText) findViewById(R.id.edtxt_phonenumber_three); 

     edtxt1.addTextChangedListener(new TextWatcher() { 
      public void afterTextChanged(Editable s) { 

       if (s.length() ==1) { 
        edtxt2.requestFocus(); 
       } 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 
      } 

      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 
      } 
     }); 

     edtxt2.addTextChangedListener(new TextWatcher() { 
      public void afterTextChanged(Editable s) { 
       if (s.length() == 1) { 
        edtxt3.requestFocus(); 
       } 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

      } 

      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 

      } 
     }); 
     edtxt3.addTextChangedListener(new TextWatcher() { 
      public void afterTextChanged(Editable s) { 
       if (s.length() == 1) { 
        edtxt1.requestFocus(); 
       } 

      } 

      public void beforeTextChanged(CharSequence s, int start, int count, 
        int after) { 

      } 

      public void onTextChanged(CharSequence s, int start, int before, 
        int count) { 

      } 
     }); 

を試してみて、XML

にすべてのExitTextandroid:maxLength="1"を設定

-1

あなたは使用すると、EditText入力を受信します。TextViewパスワードを表示します。

例:

dialog_passworld.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:paddingBottom="10dip" 
    android:paddingLeft="20dip" 
    android:paddingRight="20dip" 
    android:paddingTop="10dip" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Please enter a password" 
     android:textSize="16sp" /> 

    <FrameLayout 
     android:layout_width="match_parent" 
     android:layout_height="40dip" 
     android:layout_marginTop="15dip" > 

     <EditText 
      android:id="@+id/passcode" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <LinearLayout 
      android:id="@+id/passcode_container" 
      android:layout_width="match_parent" 
      android:layout_height="40dip" 
      android:background="@drawable/password_border" > 

      <TextView 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="@android:color/transparent" 
       android:gravity="center" 
       android:textSize="18sp" 
       android:textStyle="bold" /> 

      <View 
       android:layout_width="1px" 
       android:layout_height="match_parent" 
       android:background="@android:color/darker_gray" /> 

      <TextView 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="@android:color/transparent" 
       android:gravity="center" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 

      <View 
       android:layout_width="1px" 
       android:layout_height="match_parent" 
       android:background="@android:color/darker_gray" /> 

      <TextView 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="@android:color/transparent" 
       android:gravity="center" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 

      <View 
       android:layout_width="1px" 
       android:layout_height="match_parent" 
       android:background="@android:color/darker_gray" /> 

      <TextView 
       android:layout_width="0dip" 
       android:layout_height="match_parent" 
       android:layout_weight="1" 
       android:background="@android:color/transparent" 
       android:gravity="center" 
       android:textSize="16sp" 
       android:textStyle="bold" /> 
     </LinearLayout> 
    </FrameLayout> 

</LinearLayout> 

PasswordDialogBu​​ilder.java

public class PasswordDialogBuilder extends AlertDialog.Builder { 

    public PasswordDialogBuilder(Context context) { 
     super(context, displayMetrics, eventManager); 

     View contentView = LayoutInflater.from(context).inflate(R.layout.dialog_password, null); 

     LinearLayout passcodeContainer = (LinearLayout) contentView.findViewById(R.id.passcode_container); 

     final List<TextView> passwordViews = new ArrayList<TextView>(); 

     int childCount = passcodeContainer.getChildCount(); 
     for (int i = 0; i < childCount; i++) { 
      View childView = passcodeContainer.getChildAt(i); 
      if (childView instanceof TextView) { 
       passwordViews.add((TextView) childView); 
      } 
     } 

     final int passwordCount = passwordViews.size(); 

     EditText passwordView = (EditText) contentView.findViewById(R.id.passcode); 
     passwordView.setFilters(new InputFilter[] {new InputFilter.LengthFilter(passwordCount)}); 
     passwordView.setInputType(EditorInfo.TYPE_CLASS_NUMBER); 
     passwordView.setCursorVisible(false); 
     passwordView.setTextColor(Color.TRANSPARENT); 
     passwordView.setBackgroundColor(Color.TRANSPARENT); 
     passwordView.setSingleLine(); 

     passwordView.addTextChangedListener(new TextWatcher() { 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) {} 

      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) {} 

      @Override 
      public void afterTextChanged(Editable s) { 
       int passwordLength = s.length(); 

       for (int i = 0; i < passwordCount; i++) { 
        TextView passcodeView = passwordViews.get(i); 
        if (i < passwordLength) { 
         passcodeView.setText(String.valueOf(s.charAt(i))); 
        } else { 
         passcodeView.setText(StringUtils.EMPTY); 
        } 
       } 

       if (positiveButton != null) { 
        positiveButton.setEnabled(passwordLength == passwordCount); 
       } 
      } 
     }); 

     setView(contentView); 
    } 
} 
0

溶液コーディングがOKである、

以下のコードは、自動的に次のテキストの編集に移動し、前のテキストの編集に自動的に移動することを示します。また

それはOK、あなたが以下に+ RxBindingバインディングRxjava +のデータを使用している場合:これは私の場合のために働い

// Show button Active code when enough fields active code 
    Observable<Boolean> mObsPhoneVerify1 = RxTextView.textChanges(db.etPhoneVerify1) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .map(charSequence -> { 
       if (!charSequence.toString().equals("")) { 
        db.etPhoneVerify2.requestFocus(); 
        return true; 
       } else { 
        db.etPhoneVerify1.requestFocus(); 
        return false; 
       } 
      }); 
    Observable<Boolean> mObsPhoneVerify2 = RxTextView.textChanges(db.etPhoneVerify2) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .map(charSequence -> { 
       if (!charSequence.toString().equals("")) { 
        db.etPhoneVerify3.requestFocus(); 
        return true; 
       } else { 
        db.etPhoneVerify1.requestFocus(); 
        return false; 
       } 
      }); 
    Observable<Boolean> mObsPhoneVerify3 = RxTextView.textChanges(db.etPhoneVerify3) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .map(charSequence -> { 
       if (!charSequence.toString().equals("")) { 
        db.etPhoneVerify4.requestFocus(); 
        return true; 
       } else { 
        db.etPhoneVerify2.requestFocus(); 
        return false; 
       } 
      }); 
    Observable<Boolean> mObsPhoneVerify4 = RxTextView.textChanges(db.etPhoneVerify4) 
      .observeOn(AndroidSchedulers.mainThread()) 
      .map(charSequence -> { 
       if (!charSequence.toString().equals("")) { 
        hideKeyboard(); 
        return true; 
       } else { 
        /* 
        If enough text for all fields, no need cursor 
        Otherwise, focus previous edit text 
        */ 
        if (Utils.isValidEditText(db.etPhoneVerify1) && Utils.isValidEditText(db.etPhoneVerify2) && Utils.isValidEditText(db.etPhoneVerify3)) { 
         db.etPhoneVerify3.requestFocus(); 
        } else { 
         db.etPhoneVerify1.clearFocus(); 
         db.etPhoneVerify2.clearFocus(); 
         db.etPhoneVerify3.clearFocus(); 
         db.etPhoneVerify4.clearFocus(); 
        } 
        return false; 
       } 
      }); 

    disposable = Observable 
      .combineLatest(mObsPhoneVerify1, mObsPhoneVerify2, mObsPhoneVerify3, mObsPhoneVerify4, 
        (PhoneVerify1, PhoneVerify2, PhoneVerify3, PhoneVerify4) 
          -> PhoneVerify1 && PhoneVerify2 && PhoneVerify3 && PhoneVerify4) 
      .compose(regisObserver(false)) 
      .subscribe(aBoolean -> { 
       db.btnActiveCode.setEnabled(aBoolean); 
      }); 
0

、また、エディットテキストのMAX_LENGTHは1でなければなりません。

otp_1.addTextChangedListener(new TextWatcher() { 
       @Override 
       public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
        // 
       } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
       // 
       if(editable.length()>0){ 
        otp_1.clearFocus(); 
        otp_2.requestFocus(); 
        otp_2.setCursorVisible(true); 
       } 
      } 
     }); 

     otp_2.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
       // 
      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
       // 
       if(editable.length() > 0) { 
        otp_2.clearFocus(); 
        otp_3.requestFocus(); 
        otp_3.setCursorVisible(true); 
       } 
      } 
     }); 

     otp_3.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { 
       // 
      } 

      @Override 
      public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { 

      } 

      @Override 
      public void afterTextChanged(Editable editable) { 
       if(editable.length() > 0) { 
        otp_3.clearFocus(); 
        otp_4.requestFocus(); 
        otp_4.setCursorVisible(true); 
       } 
      } 
     });