繁体   English   中英

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

[英]Button is not calling OnClickListener with first click

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

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

 @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);
    }

你正在设置android:focusableInTouchMode="true"因此,在第一次点击它接收焦点。

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

设置这个 -

android:focusableInTouchMode="true"

对此 -

android:focusableInTouchMode="false"

在按钮上。

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

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

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

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

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

        android:text="continue"
         />

如果你的xml按钮,或img,或者衬里布局有:

android:focusableInTouchMode="true"

要解决此问题,请将其删除。

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

当你使用android:focusableInTouchMode="true"你可能不希望焦点捕获'EditText'字段。

例如:1。活动的视图包含 - > android:focusableInTouchMode 2.您打开片段对话框,但后退按钮需要单击两次才能触发它。 你应该在片段对话onCreateView()之后调用requestFocus()

暂无
暂无

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

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