Set Focus on EditText(在 EditText 上设置焦点)
问题描述
我有一个 EditText-Field 并为其设置了 OnFocusChangeListener.当它失去焦点时,会调用一个方法,该方法将 EditText 的值与数据库中的一个进行检查.如果该方法的返回值为 true,则会显示一个 toast,并且焦点应该再次回到 EditText 上.焦点应该总是回到 EditText 并且键盘应该显示,直到方法的返回值为 false.
I have an EditText-Field and set an OnFocusChangeListener for it. When it has lost focus, a method is called, which checks the value of the EditText with one in the database. If the return-value of the method is true, a toast is shown and the focus should get back on the EditText again. The focus should always get back on the EditText and the keyboard should show, until the return-value of the method is false.
我想,我的真正问题还没有完全清楚:屏幕上没有其他项目应该能够编辑,直到 EditText 的值被编辑为一个值,这使得方法checkLiganame(liganame)" 返回 false.只有 EditText-Field 应该是可编辑的.
I think, I haven't made my real problem perfectly clear yet: No other Item on the Screen should be able to edit, until the value of the EditText is edited to a value, which makes the method "checkLiganame(liganame)" return false. Only the EditText-Field should be editable.
这是我的代码(对我不起作用):
here is my code (which doesn't work for me):
final EditText Liganame = (EditText) findViewById(R.id.liganame);
Liganame.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String liganame = Liganame.getText().toString();
if (checkLiganame(liganame)) {
Toast toast = Toast.makeText(CreateTableActivity.this,
"Dieser Liganame ist bereits vergeben",
Toast.LENGTH_SHORT);
toast.show();
Liganame.requestFocus();
}
}
以及方法:
public boolean checkLiganame(String liganame) {
boolean found = false;
DatabaseHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query("liga", new String[] { "liganame" },
"liganame = '" + liganame + "'", null, null, null, null);
Log.i("Liganame: ", String.valueOf(cursor));
db.close();
if (cursor != null) {
found = true;
}
return found;
}
这段代码导致如下结果:EditText失去焦点后,焦点跳回EditText,但我不能再编辑文本了.
This code leads to the following result: After the EditText has lost focus, the focus jumps back to EditText, but I can't edit the text anymore.
更改了我的代码.场景:
Changed my code. Scenario:
我单击第一个 EditText 并将一个字符串放入其中,该字符串已在数据库中.吐司正在展示.现在我不能再编辑我的字符串了.我单击键盘上的下一步",焦点停留在第一个 EditText 上.我尝试编辑我的字符串,但没有任何反应.相反,我的新字符串显示在第二个 EditText 中.我单击设备的后退箭头,然后重新单击第一个和第二个 EditText --> 没有显示键盘.
I click on the first EditText and put a String in it, which is already in the database. The toast is showing. Now I can't edit my String anymore. I click "next" on the keyboard and the focus stays on the first EditText. I try to edit my String, but nothing happens. Instead my new String is showing in the second EditText. I click on the back-arrow of my device and reclick on the first and second EditText --> no keyboard is showing.
这是我的新代码:
public class CreateTableActivity extends Activity implements
OnFocusChangeListener {
private EditText Liganame, Mannschaftsanzahl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_league);
Liganame = (EditText) findViewById(R.id.liganame);
Liganame.setOnFocusChangeListener(this);
Mannschaftsanzahl = (EditText) findViewById(R.id.mannschaftsanzahl);
Mannschaftsanzahl.setOnFocusChangeListener(this);
final Button save_button = (Button) findViewById(R.id.create_tabelle_speichern_button);
OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
ButtonClick();
}
};
save_button.setOnClickListener(mCorkyListener);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
String liganame = Liganame.getText().toString();
if (checkLiganame(liganame)) {
if (Liganame.requestFocus()) {
getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Mannschaftsanzahl.clearFocus();
Toast.makeText(CreateTableActivity.this,
"Dieser Liganame ist bereits vergeben",
Toast.LENGTH_SHORT).show();
}
}
}
推荐答案
只要把这一行放在你的 onCreate()
Just put this line on your onCreate()
editText.requestFocus();
这篇关于在 EditText 上设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 EditText 上设置焦点
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01