我读到了关于如何设置一个对象的问题,但我似乎无法找到我想要做的答案.
使用On Focus Listener我已完成以下操作:
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0,boolean arg1) {
// TODO Auto-generated method stub
if (!arg1) {
if (Ehour.getText().length()<=0) {
Toast.makeText(getApplicationContext(),"No Hour Entered",Toast.LENGTH_LONG).show();
Calendar nohour = Calendar.getInstance();
Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
Ehour.setFocusable(true);
Ehour.requestFocus();
}
}
}});
我试过以下帖子的建议:
我的目标是当编辑文本焦点丢失时,如果给出的条目无效,我需要返回到它.
此外,根据比尔莫特的建议,我也尝试过:
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0,Toast.LENGTH_LONG).show();
Calendar nohour = Calendar.getInstance();
Ehour.setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
((EditText)arg0).requestFocus();
}
}
}});
**编辑**
我甚至在requestFocus()之前添加了以下行:
((EditText)arg0).setFocusable(true);
***编辑****
以下更改:
在第一个布局标题的布局中,我添加了:
android:id="@+id/enterdata"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
然后在OnChangeFocusListener中我这样做了:
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0,boolean arg1) {
// TODO Auto-generated method stub
if (!arg1) {
if (((EditText)arg0).getText().length()<=0) {
Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show();
Calendar nohour = Calendar.getInstance();
((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
findViewById(R.id.enterdata).requestFocus();
((EditText)arg0).setFocusable(true);
((EditText)arg0).setFocusableInTouchMode(true);
((EditText)arg0).requestFocus();
}
}
}});
然而,我仍然得到与以前相同的行为,焦点所依据的编辑文本保持这种方式,但我希望焦点去的编辑文本看起来很集中,但它似乎无法使用,好像它仍在等待成为聚焦.
***编辑****
下面的代码根据stackoverflow上发现的最近帖子,提出问题的用户遇到了与我相同的问题:
Ehour.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0,boolean arg1) {
// TODO Auto-generated method stub
if (!arg1) {
if (((EditText)arg0).getText().length()<=0) {
Calendar nohour = Calendar.getInstance();
((EditText)arg0).setText(numberformatter(nohour.get(Calendar.HOUR_OF_DAY)));
if (((EditText)arg0).requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Emin.clearFocus();
Toast.makeText(getApplicationContext(),Toast.LENGTH_LONG).show();
}
}
}
}});