Reputation: 716
I got a problem with requestFocus.
First, this is my source code:
<EditText
android:id="@+id/etGLNum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btGLCalculate"
android:ems="10"
android:hint="@string/gl_num2_hint"
android:inputType="number" />
<EditText
android:id="@+id/etGLNum1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/etGLNum2"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/btGLCalculate"
android:ems="10"
android:hint="@string/gl_num1_hint"
android:inputType="number">
<requestFocus />
</EditText>
<TextView
android:id="@+id/tvGLResult"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/etGLNum2"
android:gravity="center"
android:text="@string/result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/btGLCalculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/tvGLResult"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/etGLNum1"
android:text="@string/calculate" />
As you see, focus have to go to etGLNum1, but it always goes to etGLNum2.
I already tried adding etGLNum1.requestFocus();
from my java file but it didn't worked.
What should i do?
Thanks :)
Upvotes: 6
Views: 13527
Reputation: 393
With what i have experienced is, Android have a a default behavior of giving focus to the first focusable element found in the current surface(if we have not set any default element to focusable).
You can use etGLNum1.setFocusable(true)
(before setContentView() call) then etGLNum1.requestFocus()
(after setContentView() call) from your onCreate()..
It should work..
Upvotes: 0
Reputation: 3261
Add requestFocus
in your Edittext
.
<EditText
android:id="@+id/etGLNum2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btGLCalculate"
android:ems="10"
android:hint="@string/gl_num2_hint"
android:inputType="number">
<requestFocus />
</EditText>
Upvotes: 2
Reputation: 52
In your onCreate()
write:
EditText etGLNum1 = (EditText)findViewById(R.id.etGLNum1);
etGLNum1.requestFocus();
etGLNum1.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus)
{
//enter code here
}
});
Upvotes: 0
Reputation: 6444
try this :-
etGLNum2.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View rv, boolean hasFocus) {
etGLNum2.clearFocus();
etGLNum1.requestFocus();
}
}
});
Upvotes: 1
Reputation: 2113
just add xml all editexts:
<EditText
android:id="@+id/editText1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:hint="@string/gl_num2_hint"
android:inputType="number" >
<requestFocus />
</EditText>
Upvotes: 0