首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >按钮没有在第一次单击时调用OnClickListener

按钮没有在第一次单击时调用OnClickListener
EN

Stack Overflow用户
提问于 2013-12-09 22:41:56
回答 5查看 12.1K关注 0票数 19

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

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

回答 5

Stack Overflow用户

回答已采纳

发布于 2013-12-10 18:56:47

是的,是的,我得到了答案,经过大量的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"
         />
票数 9
EN

Stack Overflow用户

发布于 2013-12-09 22:45:05

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

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

票数 27
EN

Stack Overflow用户

发布于 2013-12-12 21:26:29

把这个设置成-

android:focusableInTouchMode="true"

对这个-

android:focusableInTouchMode="false"

按下按钮。

解释-如果你想让按钮可聚焦,那么在第一次点击时,焦点就会传递给按钮,那么在第二次触摸时,点击就会传递。EditText是一个可聚焦的视图,它首先获得焦点,因此其他视图必须首先从EditText重新获得焦点,除非它们不需要焦点来工作,就像按钮一样。如果你只需要OnClick函数,那么你不需要焦点,所以你可以多点击一次。

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

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20473355

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档