简体   繁体   English

按钮不是第一次单击调用OnClickListener

[英]Button is not calling OnClickListener with first click

I am trying to develop a Log in 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 activity_login.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 loginActivity.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);
    }

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侦听器?

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. EditText是一个可聚焦的视图,它首先获得焦点,因此其他视图必须首先从EditText重新获得焦点,除非它们不需要焦点工作,就像按钮一样。 If you just need the OnClick function, then you don't need focus, so you can spre one extra click. 如果您只需要OnClick功能,那么您不需要焦点,因此您可以额外点击一下。

PS: Although it shouldn't require, but setting android:focusable to false will help too, if the first one doesn't work. PS:虽然它不应该要求,但设置android:focusable为false也会有所帮助,如果第一个不起作用。

Yes Yes I got the answer, after a lot of RND, I got the solution, I just need to implement setOnClickListener(), and setOnFocusChangeListener(). 是的我得到了答案,经过大量的RND后,我得到了解决方案,我只需要实现setOnClickListener()和setOnFocusChangeListener()。 So I am putting here the solution. 所以我在这里提出解决方案。

ActivityLogin.java ActivityLogin.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 activity_login.xml

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

        android:text="continue"
         />

if in your xml in button, or img, or liner layout has: 如果你的xml按钮,或img,或者衬里布局有:

android:focusableInTouchMode="true"

To resolve, just remove this. 要解决此问题,请将其删除。

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. 例如:1。活动的视图包含 - > android:focusableInTouchMode 2.您打开片段对话框,但后退按钮需要单击两次才能触发它。 3. You should call requestFocus() after the Fragment Dialog onCreateView() 你应该在片段对话onCreateView()之后调用requestFocus()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM