ChatGPT解决这个技术问题 Extra ChatGPT

在 EditText 上设置焦点

我有一个 EditText-Field 并为它设置了一个 OnFocusChangeListener。当它失去焦点时,会调用一个方法,该方法将 EditText 的值与数据库中的一个进行检查。如果该方法的返回值为 true,则会显示一个 toast,并且焦点应该再次回到 EditText 上。焦点应该总是回到 EditText 并且键盘应该显示,直到方法的返回值为 false。

编辑:我想,我的真正问题还没有完全清楚:屏幕上没有其他项目应该能够编辑,直到 EditText 的值被编辑为一个值,这使得方法“checkLiganame(liganame) "返回假。只有 EditText-Field 应该是可编辑的。

这是我的代码(对我不起作用):

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,但我无法再编辑文本。

EDIT2:更改了我的代码。设想:

我单击第一个 EditText 并在其中放入一个字符串,该字符串已经在数据库中。吐司正在展示。现在我不能再编辑我的字符串了。我单击键盘上的“下一步”,焦点停留在第一个 EditText 上。我尝试编辑我的字符串,但没有任何反应。相反,我的新字符串显示在第二个 EditText 中。我单击设备的后退箭头,然后重新单击第一个和第二个 EditText --> 没有显示键盘。

这是我的新代码:

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();
        }
    }
}
如果以下答案对您有帮助,请采纳其中一个答案

C
Christopher Moore

只需将此行放在您的 onCreate()

editText.requestFocus();

它还会自动激活虚拟键盘的打开。所以你不需要在代码中这样做。
它没有在我的机箱上打开键盘。
只需在上面的代码下面添加这一行=> InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);
D
Darwind

请求焦点不足以显示键盘。

要获得焦点并显示键盘,您可以编写如下内容:

if(myEditText.requestFocus()) {
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}

编辑:在添加 checkLiganame 方法后向答案添加额外信息。

在 checkLiganame 方法中,您检查游标是否为空。游标将始终返回一个对象,因此检查 null 不会做任何事情。但是问题出在 db.close();

当您关闭数据库连接时,Cursor 变为无效并且很可能为空。

因此,在您获取值后关闭数据库。

与其检查游标是否为空,不如检查返回的行数是否大于 0:if(cursor.getCount() > 0),如果是,则将布尔值设置为 true。

EDIT2:所以这里有一些如何使它工作的代码。 EDIT3:对不起,我添加了错误的代码......;S

首先,如果另一个 EditText 获得焦点,您需要清除焦点。这可以通过 myEditText.clearFocus() 完成。然后在你的 onFocusChangeListener 中你不应该真正关心第一个 EditText 是否有焦点,所以 onFocusChangeListener 可能看起来像这样:

public class MainActivity extends Activity implements OnFocusChangeListener {
    private EditText editText1, editText2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        editText1 = (EditText) findViewById(R.id.editText1);
        editText1.setOnFocusChangeListener(this);
        editText2 = (EditText) findViewById(R.id.editText2);
        editText2.setOnFocusChangeListener(this);
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        String liganame = editText1.getText().toString();

        if(liganame.length() == 0) {
            if(editText1.requestFocus()) {
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                editText2.clearFocus();
                Toast.makeText(MainActivity.this, "Dieser Liganame ist bereits vergeben", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

将第一个检查 if(liganame.length() == 0) 替换为您自己的检查,然后它应该可以工作。请注意,所有 EditText 视图都应该将它们的 onFocusChangeListener 设置为同一个侦听器,就像我在示例中所做的那样。


到目前为止谢谢。但这并不能解决我的全部问题。我编辑了我的问题的描述。
那么 checkLiganame 到底在做什么呢?为什么你不能只检查 if(liganame.isEmpty()) 如果它是真的,再次 requestFocus ?
谢谢 :-) 现在我的方法工作正常 :-) 但是我的 EditText-Field 的主要问题还没有解决。在方法找到一行之后,我仍然无法编辑它......
所以你添加了 if(myEditText.requestFocus()) { getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);在 onFocusChanged 监听器里面?
是的,我做到了。在 Liganame.requestFocus();.我点击键盘上的“下一步”,跳转到下一个 EditText-Field。吐司正在显示,重点是两个 EditText-Fields。但我只能编辑第二个。第一个,在这种情况下应该是唯一具有焦点的,不再可编辑。
G
Groco

Darwind 代码没有显示键盘。

这对我有用:

        _searchText.requestFocus();
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);

如果键盘未显示,请尝试强制:

        imm.showSoftInput(_searchText, InputMethodManager.SHOW_FORCED);

P
Pang

这对我有用:

public void showKeyboard(final EditText ettext){
    ettext.requestFocus();
    ettext.postDelayed(new Runnable(){
            @Override public void run(){
                InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
                keyboard.showSoftInput(ettext,0);
            }
        }
        ,200);
}

隐藏:

private void hideSoftKeyboard(EditText ettext){
    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(ettext.getWindowToken(), 0);
}

为什么需要等待 200 毫秒?有必要还是我可以立即展示?
C
Cactus

单击按钮时,这会更改 EditText 的焦点:

public class MainActivity extends Activity {
    private EditText e1,e2;
    private Button b1,b2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        e1=(EditText) findViewById(R.id.editText1);
        e2=(EditText) findViewById(R.id.editText2);
        e1.requestFocus();
        b1=(Button) findViewById(R.id.one);
        b2=(Button) findViewById(R.id.two);
        b1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                e1.requestFocus();

            }
        });
        b2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                e2.requestFocus();
            }
        });
    }
}

B
Berni Gf

这对我有用,设置焦点并显示键盘

EditText userNameText = (EditText) findViewById(R.id.textViewUserNameText);
userNameText.setFocusable(true);
userNameText.setFocusableInTouchMode(true);
userNameText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(userNameText, InputMethodManager.SHOW_IMPLICIT);

谢谢。 setFocusable 需要 API 26。在我的情况下也不需要 setFocusableInTouchMode
S
Sudhakar

如果我们动态创建 EditText,那么我们必须设置 requestFocus(),如下所示。

    EditText editText = new EditText(this);
    editText.setWidth(600);
    editText.requestFocus();

如果我们已经在 xml 视图中声明了该组件,那么我们必须找到它,我们可以如下所示关注焦点。

EditText e1=(EditText) findViewById(R.id.editText1);
e1.requestFocus();

它只将焦点设置到相应的 EditText 组件。


B
Barakzai
    mEditText.setFocusableInTouchMode(true);
    mEditText.requestFocus();

    if(mEditText.requestFocus()) {
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

H
Hari Prasad
    Button btnClear = (Button) findViewById(R.id.btnClear);

    EditText editText1=(EditText) findViewById(R.id.editText2);
    EditText editText2=(EditText) findViewById(R.id.editText3);

    btnClear.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {

            editText1.setText("");
            editText2.setText("");

            editText1.requestFocus();
        }
    });

M
Mücahit Şenol

如果您尝试在布局膨胀之前调用 requestFocus(),它将返回 false。此代码在布局膨胀后运行。您不需要如 above 所述的 200 毫秒延迟。

editText.post(Runnable {
   if(editText.requestFocus()) {
       val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
       imm?.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
   }
})

佚名
 private void requestFocus(View view) {
        if (view.requestFocus()) {
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        }
    }

//Function Call
requestFocus(yourEditetxt);

M
M B

你可以用一行来做到这一点:

yourEditText.RequestFocusFromTouch();

实际上,这是唯一对我有用的答案。它就像我想要的那样模拟触摸。
c
c.lamont.dev

对于 Xamarin.Android,我创建了这个扩展。

public static class ViewExtensions
{
    public static void FocusEditText(this EditText editText, Activity activity)
    {
        if (editText.RequestFocus())
        {
            InputMethodManager imm = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
            imm.ShowSoftInput(editText, ShowFlags.Implicit);
        }
    }
}

Y
Yogesh Umesh Vaity

如果在 onCreate() 中使用 requestFocus() 会导致键盘在点击时不显示的问题,请使用 BindingAdapter 并在其中请求焦点。

这是如何做到的:

绑定适配器

@BindingAdapter("requestFocus")
fun bindRequestFocus(editText: EditText, event: Event<Boolean>?) {
    event?.getContentIfNotHandled()?.let {
        if (it) editText.requestFocus()
    }
}

T
Thành Thỏ

我的回答here

正如我在官方文档上阅读的那样,我认为这是最好的答案,只需将 View 传递给 EditText 等参数,但 showSoftKeyboard 似乎不适用于横向

private fun showSoftKeyboard(view: View) {
    if (view.requestFocus()) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
    }
}

private fun closeSoftKeyboard(view: View) {
    if (view.requestFocus()) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
    }
}

U
Undisputed

我不知道您是否已经找到了解决方案,但是对于再次请求焦点后的编辑问题:

您是否尝试在您的 edittext1 上调用方法 selectAll()setSelection(0)(如果是 emtpy)?

请让我知道这是否有帮助,因此我将编辑我的答案以提供完整的解决方案。


u
user5176384
new OnEditorActionListener(){
   @Override
   public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
      editText.requestFocus();
      //used ******* return true ******
      return **true**;
   }
} 

T
Trần Thị Diệu My

请在清单上尝试此代码

<activity android:name=".EditTextActivity" android:windowSoftInputMode="stateAlwaysVisible">
</activity>