Java Button 没有在第一次点击时调用 OnClickListener

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20473355/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 02:12:16  来源:igfitidea点击:

Button is not calling OnClickListener with first click

javaandroidxmlbutton

提问by Amit Jayaswal

I am trying to develop a Login page, where I have Username, password and a button. When I click the button for the first time, nothing happens, but when I click the button for the second time, then it works properly. I am confused, why it is happening ?

我正在尝试开发一个登录页面,其中有用户名、密码和一个按钮。当我第一次单击该按钮时,没有任何反应,但是当我第二次单击该按钮时,它可以正常工作。我很困惑,为什么会这样?

activity_login.xml

活动登录.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_bg"
tools:context=".LoginActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:orientation="vertical"
android:layout_margin="30dp" >

<EditText
    style="@style/EditText1"
    android:id="@+id/userEditText"
    android:layout_marginBottom="50dp"
    android:inputType="textNoSuggestions"
    android:singleLine="true"
    android:hint="username" />
 <EditText
    style="@style/EditText1"
    android:id="@+id/passEditText"
    android:inputType="textPassword"
    android:layout_marginBottom="50dp"
    android:hint="password" />
<Spinner
    android:id="@+id/locationSpinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:popupBackground="#ffffff"
    style="@style/EditText1"
   android:layout_marginBottom="50dp" />
<Button
   style="@style/Button1"
   android:focusable="true"
   android:focusableInTouchMode="true"
   android:onClick="onLoginClick"
   android:text="continue"
         />

loginActivity.java

登录活动.java

 @SuppressLint("DefaultLocale")
    public void onLoginClick(View view) {
        String username = mUserEditText.getText().toString();
        String password = mPassEditText.getText().toString();
        String location = mLocationData.get(
                mLocationSpinner.getSelectedItemPosition()).toLowerCase();
        if (username.isEmpty() || password.isEmpty()) {
            CreatorMessenger
                    .getInstance()
                    .showMessage(this, "Error!!",
                            "You need to enter username and password both to continue!!");
            return;
        }
        User user;
        user = new User(username);/*
                             * }
                                 */
        user.setLocation(location);
        AppManager.getInstance().setLoggedInUser(user);

        APICaller.getInstance().login(username, password, location);
    }

采纳答案by Amit Jayaswal

Yes Yes I got the answer, after a lot of RND, I got the solution, I just need to implement setOnClickListener(), and setOnFocusChangeListener(). So I am putting here the solution.

是的,我得到了答案,经过大量的 RND,我得到了解决方案,我只需要实现 setOnClickListener() 和 setOnFocusChangeListener()。所以我把解决方案放在这里。

ActivityLogin.java

活动登录.java

 buttonLogin= (Button) findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("hello", "hellow");
            String username = mUserEditText.getText().toString();
            String password = mPassEditText.getText().toString();
            String location = mLocationData.get(mLocationSpinner.getSelectedItemPosition()).toLowerCase();
            if(username.isEmpty()||password.isEmpty()){
            popbox();
                return;
            }
            User user;
            user = new User(username);
            user.setLocation(location);
            AppManager.getInstance().setLoggedInUser(user);
            APICaller.getInstance().login(username, password, location);
        }
    });
    buttonLogin.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    public void onFocusChange(View v, boolean hasFocus) {
        if (hasFocus) {
            v.performClick();
        }
    }
});
}

activity_login.xml

活动登录.xml

  <Button
        android:id="@+id/buttonLogin"
        style="@style/Button1"

        android:text="continue"
         />

回答by Aniket Thakur

You are setting android:focusableInTouchMode="true"Hence on 1st click it receives focus.

您正在设置android:focusableInTouchMode="true"因此在第一次单击时它会获得焦点。

Refer Android button - How to set focusable to true and still accept onClick listener on first click?

参考Android 按钮 - 如何将 focusable 设置为 true 并在第一次单击时仍然接受 onClick 侦听器?

回答by noob

Set this -

设置这个 -

android:focusableInTouchMode="true"

to this -

对此——

android:focusableInTouchMode="false"

on button.

在按钮上。

Explanation - If you'll make the button focusable then on first click the focus is passed to the button, then the click is passed on second touch. EditText is a focusable View which gains focus first and therefore other views must first regain the focus from EditText, unless they do not need focus to work, just like buttons. If you just need the OnClick function, then you don't need focus, so you can spre one extra click.

说明 - 如果您将按钮设为可聚焦,则在第一次单击时焦点将传递给按钮,然后单击将在第二次触摸时传递。EditText 是一个可聚焦的视图,它首先获得焦点,因此其他视图必须首先从 EditText 重新获得焦点,除非它们不需要焦点来工作,就像按钮一样。如果您只需要 OnClick 功能,那么您就不需要焦点,因此您可以额外单击一次。

PS: Although it shouldn't require, but setting android:focusableto false will help too, if the first one doesn't work.

PS:虽然它不应该要求,但android:focusable如果第一个不起作用,设置为 false 也会有帮助。

回答by Guihgo

if in your xml in button, or img, or liner layout has:

如果在您的 xml in button、img 或 liner 布局中具有:

android:focusableInTouchMode="true"

To resolve, just remove this.

要解决,只需删除它。

回答by Kenneth Tse

I suggested to use request focus in the "onCreate()" method.

我建议在“onCreate()”方法中使用请求焦点。

When you using android:focusableInTouchMode="true"you may don't want the focus catch by an 'EditText' field.

当您使用时,android:focusableInTouchMode="true"您可能不希望焦点被“EditText”字段捕获。

For example: 1. An activity has a view contain -> android:focusableInTouchMode 2. You open a Fragment Dialog, but the back button need click two times to fire it. 3. You should call requestFocus() after the Fragment Dialog onCreateView()

例如: 1. 一个活动有一个视图包含 -> android:focusableInTouchMode 2. 你打开一个 Fragment Dialog,但返回按钮需要点击两次才能触发它。3. 你应该在 Fragment Dialog onCreateView() 之后调用 requestFocus()