Reputation: 57
I create an application on android studio,I have 4 EditText to write an IP Address, I want to move automaticly the cursor to the following EditText when I write 3 numbers in an EditText. How to do Thanks You?
Upvotes: 0
Views: 318
Reputation: 57
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AbsoluteLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:focusable="true"
android:focusableInTouchMode="false"
android:paddingEnd="3dp">
<Button
android:id="@+id/IdOK"
android:layout_width="118dp"
android:layout_height="wrap_content"
android:layout_x="123dp"
android:layout_y="256dp"
android:text="OK" />
<TextView
android:layout_width="178dp"
android:layout_height="36dp"
android:text="Saisir l'adresse IP"
android:id="@+id/saisiAdIp"
android:layout_x="96dp"
android:layout_y="105dp"
android:textSize="21dp" />
<EditText
android:layout_width="60dp"
android:layout_height="40dp"
android:id="@+id/AdIpsaisi1"
android:layout_x="35dp"
android:layout_y="146dp"
android:inputType="number"
android:maxLength="3"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusForward="@+id/AdIpsaisi2" />
<EditText
android:layout_width="60dp"
android:layout_height="40dp"
android:id="@+id/AdIpsaisi2"
android:layout_x="103dp"
android:layout_y="146dp"
android:inputType="number"
android:maxLength="3"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusRight="@+id/AdIpsaisi3"/>
<EditText
android:layout_width="60dp"
android:layout_height="40dp"
android:id="@+id/AdIpsaisi3"
android:layout_x="170dp"
android:layout_y="146dp"
android:inputType="number"
android:maxLength="3"
android:focusable="true"
android:focusableInTouchMode="true"
android:nextFocusRight="@+id/AdIpsaisi4"/>
<EditText
android:layout_width="60dp"
android:layout_height="40dp"
android:id="@+id/AdIpsaisi4"
android:layout_x="240dp"
android:layout_y="146dp"
android:inputType="number"
android:maxLength="3" />
<TextView
android:layout_width="10dp"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/textView"
android:layout_x="96dp"
android:layout_y="165dp" />
<TextView
android:layout_width="10dp"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/textView6"
android:layout_x="165dp"
android:layout_y="165dp" />
<TextView
android:layout_width="10dp"
android:layout_height="wrap_content"
android:text="."
android:id="@+id/textView7"
android:layout_x="231dp"
android:layout_y="165dp" />
<TextView
android:layout_width="184dp"
android:layout_height="87dp"
android:text="Adresse IP incorrect"
android:id="@+id/erreursaisi"
android:layout_x="116dp"
android:layout_y="374dp"
android:textSize="30dp" />
</AbsoluteLayout>
Upvotes: 0
Reputation: 325
You have to constantly check the input of each editText in
onTextChanged method, To do this, you have to implement
addTextChangedListener on each editText and check the charSequence if
equal = 3.
for example
yourFirstEditText.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) {
if(charSequence.toString().length() == 3) {
//call next forcus
yourSecondEditText.requestFocus();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
yourSecondEditText.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) {
if(charSequence.toString().length() == 3) {
//call next forcus
yourThirdEditText.requestFocus();
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
//you don't need to do that for the last editText
Upvotes: 1
Reputation: 374
There are multiple ways to do this.
Set the inputType and maxLength attributes in xml of each of these EditTexts.
android:inputType="number" android:maxLength="3"
Next, define the next focus item:
android:nextFocusRight="id"
Alternately you can add a textChangedListener for each of the EditText's:
editText1.addTextChangedListener(mTextWatcher);
private final TextWatcher mTextWatcher = 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) {
if (s.length() == 3) {
// editText2.requestFocus();
}
};
Try this, also u need to add two attributes to the xml and each views as well:
android:focusable="true";
android:focusableInTouchMode="true"
Upvotes: 1