我有一个简单的登录布局,其中包含两个 EditText 字段和一个登录按钮。问题是当软键盘打开时,ActionBar 消失了,我将焦点从 EditText 更改为 Button,当我按下返回时,ActionBar 又回来了。当软键盘关闭并且我使用 DPAD 浏览 EditTexts 和 Button 时,问题不会发生。
我使用ActionBarSherlock,问题只出现在Android 2.x 模拟器上,在4.x 模拟器上一切正常。我知道 ActionBarSherlock 在可用的 Android 版本上使用本机 ActionBar 实现,因此这可能是 ActionBarSherlock 代码的问题。
我还执行了一个测试来检查 的值ActionBar.isShowing()
,但即使 ActionBar 在屏幕上不可见,它也会返回 true。
我不知道在这种情况下发生了什么,有人有什么想法吗?
布局 XML
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:ignore="UselessParent" >
<LinearLayout
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical" >
<EditText
android:id="@+id/username"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/username"
android:inputType="textNoSuggestions"
android:nextFocusUp="@+id/loginButton"
android:imeOptions="actionNext" />
<EditText
android:id="@+id/password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:hint="@string/password"
android:inputType="textPassword"
android:imeOptions="actionDone" />
<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/login"
android:textSize="20sp"
android:nextFocusDown="@+id/username" />
</LinearLayout>
</RelativeLayout>
片段代码
public class LoginFragment extends BaseFragment {
@InjectView(R.id.loginButton) protected Button mLoginButton;
@InjectView(R.id.username) protected EditText mUsernameEditText;
@InjectView(R.id.password) protected EditText mPasswordEditText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.login, container, false);
}
@Override
public void onStart() {
super.onStart();
mLoginButton.setEnabled(allFieldsValid());
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleLogin();
}
});
mPasswordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE && allFieldsValid()) {
handleLogin();
}
return false;
}
});
TextWatcher fieldValidatorTextWatcher = new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mLoginButton.setEnabled(allFieldsValid());
}
};
mUsernameEditText.addTextChangedListener(fieldValidatorTextWatcher);
mPasswordEditText.addTextChangedListener(fieldValidatorTextWatcher);
}
private void handleLogin() {
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(true);
Intent intent = new Intent(getActivity(), LoginService.class);
intent.putExtra(BaseIntentService.EXTRA_STATUS_RECEIVER, mResultReceiver);
intent.putExtra(LoginService.PARAM_USERNAME, mUsernameEditText.getText().toString());
intent.putExtra(LoginService.PARAM_PASSWORD, mPasswordEditText.getText().toString());
getActivity().startService(intent);
}
private boolean allFieldsValid() {
return usernameFieldIsValid() && passwordFieldIsValid();
}
private boolean usernameFieldIsValid() {
return !TextUtils.isEmpty(mUsernameEditText.getText());
}
private boolean passwordFieldIsValid() {
return !TextUtils.isEmpty(mPasswordEditText.getText());
}
@Override
public void onReceiveResult(int resultCode, Bundle resultData) {
getSherlockActivity().setSupportProgressBarIndeterminateVisibility(false);
super.onReceiveResult(resultCode, resultData);
}
@Override
public void onReceiveResultSuccess(Bundle resultData) {
((LoginActivity) getActivity()).redirectToSelectTeamwebActivity(resultData.getInt(LoginService.RESULT_USER_ID));
}
@Override
public void onReceiveResultFailure(Bundle resultData) {
mPasswordEditText.setText("");
String errorMessage = getString(R.string.invalid_login_credentials);
Toast.makeText(getActivity(), errorMessage, Toast.LENGTH_LONG).show();
}
}