【问题标题】:ActionBarSherlock actionbar disappears when focus changes from EditText to Button当焦点从 EditText 更改为 Button 时,ActionBarSherlock 操作栏消失
【发布时间】:2012-04-24 01:45:52
【问题描述】:

我有一个简单的登录布局,其中包含两个 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();
    }

}

【问题讨论】:

  • 我目前正在开车,但听起来您可能在清单中指定了平移视图而不是调整其大小的选项
  • 这确实是问题所在,我为此活动添加了 android:windowSoftInputMode="stateUnspecified|adjustResize" 到清单节点,现在 ActionBar 停留在屏幕上。谢谢你的帮助!如果你写下这个问题的答案,我会接受,否则我会在一段时间后回答我自己的问题。

标签: android actionbarsherlock


【解决方案1】:

ActionBarSherlock 在 pre-ICS 上的内容视图内而不是在窗口的装饰视图中附加一个兼容性操作栏。正因为如此,它很容易受到更多的不便,这可能会导致像您所看到的那样的意外行为。

如果您已将 windowSoftInputMode 设置为在 IME 打开时平移内容视图,则操作栏将从屏幕上消失。解决此问题的简单方法是使用不同的模式(例如,调整大小),这将重新布局内容视图并将操作栏保持在屏幕上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多