在 EditText 上设置焦点

Set Focus on EditText(在 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 上设置焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)
cocos2d-android: how to display score(cocos2d-android:如何显示分数)
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)
Android file copy(安卓文件拷贝)
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)