巴顿并没有叫OnClickListener与第一次点击 [英] Button is not calling OnClickListener with first click

查看:192
本文介绍了巴顿并没有叫OnClickListener与第一次点击的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个 登录 页面,在那里我有用户名,密码和一个按钮。当我按一下按钮,第一次,什么都不会发生,但是当我点击该按钮第二次,然后它工作正常。我很困惑,为什么会发生?

  

activity_login.xml

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:背景=@可绘制/ splash_bg
工具:上下文=。LoginActivity>
<的LinearLayout
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:方向=垂直
机器人:layout_margin =30dp>

<的EditText
    风格=@风格/ EditText1
    机器人:ID =@ + ID / userEditText
    机器人:layout_marginBottom =50dp
    机器人:inputType =textNoSuggestions
    机器人:单线=真
    机器人:提示=用户名/>
 <的EditText
    风格=@风格/ EditText1
    机器人:ID =@ + ID / passEditText
    机器人:inputType =textPassword
    机器人:layout_marginBottom =50dp
    机器人:提示=密码/>
<微调
    机器人:ID =@ + ID / locationSpinner
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:popupBackground =#FFFFFF
    风格=@风格/ EditText1
   机器人:layout_marginBottom =50dp/>
<按钮
   风格=@风格/ Button1的
   机器人:可聚焦=真
   机器人:focusableInTouchMode =真
   机器人:的onClick =onLoginClick
   机器人:文本=继续
         />
 

loginActivity.java

  @燮pressLint(DefaultLocale)
    公共无效onLoginClick(查看视图){
        字符串username = mUserEditText.getText()的toString()。
        。字符串password = mPassEditText.getText()的toString();
        字符串位置= mLocationData.get(
                mLocationSpinner.getSelectedItemPosition())与toLowerCase()。
        如果(username.isEmpty()|| password.isEmpty()){
            CreatorMessenger
                    .getInstance()
                    .showMessage(这一点,错误!,
                            您需要输入用户名和密码,既要继续!);
            返回;
        }
        用户的用户;
        用户=新用户(用户名); / *
                             *}
                                 * /
        user.setLocation(位置);
        。AppManager.getInstance()setLoggedInUser(用户);

        APICaller.getInstance()登录(用户名,密码,位置)。
    }
 

解决方案

是的是的,我得到了答案,很多RND后,我得到了解决,我只需要实现s​​etOnClickListener(),和setOnFocusChangeListener()。所以,我把这里的解决方案。

  

ActivityLogin.java

  buttonLogin =(按钮)findViewById(R.id.buttonLogin);
    buttonLogin.setOnClickListener(新OnClickListener(){
        @覆盖
        公共无效的onClick(视图v){
            Log.d(你好,hellow);
            字符串username = mUserEditText.getText()的toString()。
            。字符串password = mPassEditText.getText()的toString();
            字符串位置= mLocationData.get(mLocationSpinner.getSelectedItemPosition())与toLowerCase()。
            如果(username.isEmpty()|| password.isEmpty()){
            popbox();
                返回;
            }
            用户的用户;
            用户=新用户(用户名);
            user.setLocation(位置);
            。AppManager.getInstance()setLoggedInUser(用户);
            APICaller.getInstance()登录(用户名,密码,位置)。
        }
    });
    buttonLogin.setOnFocusChangeListener(新View.OnFocusChangeListener(){
    公共无效onFocusChange(视图V,布尔hasFocus){
        如果(hasFocus){
            v.performClick();
        }
    }
});
}
 

  

activity_login.xml

 <按钮
        机器人:ID =@ + ID / buttonLogin
        风格=@风格/ Button1的

        机器人:文本=继续
         />
 

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

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

解决方案

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.

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"
         />

这篇关于巴顿并没有叫OnClickListener与第一次点击的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆