Android版的EditText onclick事件处理和选择 [英] Android edittext onclick event handling and selection
问题描述
我有两个编辑文本的意见。如果我点击第一个,我需要选择要首先的EditText并设置为第二个00。就像在默认的Android闹钟。
我的问题:
I have two edit text views. If I click first, I need to select first edittext and set to second "00". Like in default android alarm clock. My problem:
- 我有API级别10,所以我不会写这样的:
firstEText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
secondEText.setText("00");
}
});
如果我用
firstEText.setOnKeyListener(new View.OnKeyListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
secondEText.setText("00");
}
});
所以我需要点击两次我的看法。可能的解决方法:
so I need click my view twice. Possible solution:
firstEText.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
//but with onTouch listener I have problems with
//edit text selection:
((EditText) view).setSelection(0, ((EditText) view).getText().length());
}
return false;
}
});
所以我.setSelection并不总是工作。我的天啊!请帮助我
so my .setSelection does not always work. OMG! Help me please
推荐答案
如果我理解正确的,要做到以下几点:
If I understood correctly, you want to do the following:
- 对焦时
firstEText
,选择在firstEText
中的所有文本,并设置secondEText
来的00的
- When focusing
firstEText
, select all the text withinfirstEText
and setsecondEText
to "00".
我不明白的是为什么你说你不能使用 setOnFocusChangeListener
,因为,<一个href=\"http://developer.android.com/reference/android/view/View.html#setOnFocusChangeListener%28android.view.View.OnFocusChangeListener%29\"相对=nofollow>这是可用,因为API 1 。
What I don't understand is why you say you cannot use setOnFocusChangeListener
, since, it is available since API 1.
一个方便的属性来选择的的的EditText 的一个元素获取焦点的时候,是<一中的所有文本href=\"http://developer.android.com/reference/android/widget/TextView.html#setSelectAllOnFocus%28boolean%29\"相对=nofollow>安卓selectAllOnFocus ,这不正是你想要的。然后,您只需要设置 secondEText
来的00的
A convenient attribute to select all the text of an EditText when getting focus on an element, is android:selectAllOnFocus, which does exactly what you want. Then, you just need to set secondEText
to "00".
UI
<EditText
android:id="@+id/editText1"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:background="@android:color/white"
android:textColor="@android:color/black" />
<EditText
android:id="@+id/editText2"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:textColor="@android:color/black" />
活动
firstEText = (EditText) findViewById(R.id.editText1);
secondEText = (EditText) findViewById(R.id.editText2);
firstEText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
secondEText.setText("00");
}
}
});
希望它帮助。
这篇关于Android版的EditText onclick事件处理和选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!