Ami*_*wal 18 java xml android button
我正在尝试开发一个登录页面,其中我有用户名,密码和一个按钮.当我第一次单击该按钮时,没有任何反应,但是当我第二次单击该按钮时,它可以正常工作.我很困惑,为什么会这样?
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"
/>
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
Ani*_*kur 26
您正在设置android:focusableInTouchMode="true"
因此,第一次单击它会获得焦点.
请参阅Android按钮 - 如何将focusable设置为true并在第一次单击时仍接受onClick侦听器?
noo*_*oob 13
设置这个 -
android:focusableInTouchMode="true"
Run Code Online (Sandbox Code Playgroud)
对此 -
android:focusableInTouchMode="false"
Run Code Online (Sandbox Code Playgroud)
在按钮上.
说明 - 如果您将按钮设置为可聚焦,则首次单击时焦点将传递给按钮,然后在第二次触摸时传递单击.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();
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
activity_login.xml
<Button
android:id="@+id/buttonLogin"
style="@style/Button1"
android:text="continue"
/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10789 次 |
| 最近记录: |