我有一个简单的登录布局,其中包含两个EditText字段和一个要登录的Button。问题是当软键盘打开时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();
}
}
答案 0 :(得分:4)
ActionBarSherlock在内容视图内部的pre-ICS上附加兼容性操作栏,而不是在Window的装饰视图中。因此,它可能会带来更多不便,可能会导致您所看到的意外行为。
如果您在IME打开时将windowSoftInputMode
设置为平移内容视图,则操作栏将从屏幕上消失。解决这个问题的简单方法是使用不同的模式(例如,调整大小),这将重新布局内容视图并将操作栏保留在屏幕上。