2012-04-21 71 views
2

我有一个简单的登录布局,其中包含两个EditText字段和一个用于登录的按钮。问题是,当软键盘打开时,ActionBar会消失,并且我将焦点从EditText更改为Button,并且当我按下后,ActionBar会再次返回。当软键盘关闭时,不会发生该问题,并使用DPAD浏览EditTexts和Button。当焦点从EditText更改为Button时,ActionBarSherlock操作栏消失

我使用ActionBarSherlock,并且仅在Android 2.x仿真器上出现问题,在4.x仿真器上一切正常。我知道ActionBarSherlock在Android版本中使用原生ActionBar实现,因此它可能是ActionBarSherlock代码的问题。

我也执行了一个测试来检查ActionBar.isShowing()的值,但是即使ActionBar在屏幕上不可见,这个返回值也是正确的。

我不知道在这种情况下发生了什么,任何人有一些想法?

布局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> 

片段CODE

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

我目前正在开车,但它听起来像你可能有清单中指定的选项,你平移视图,而不是调整它的大小 – 2012-04-22 03:50:54

+0

这确实是问题,我添加了android:windowSoftInputMode =“stateUnspecified | adjustResize”此活动的清单节点,现在ActionBar停留在屏幕上。谢谢你的帮助!如果你写这个问题的答案,我会接受它,否则我会在一段时间后回答我自己的问题。 – 2012-04-22 09:49:39

回答

4

ActionBarSherlock重视对预ICS兼容操作栏的内容视图的内部,而不是在窗口的装饰视图。正因为如此,它容易出现更多不便之处,从而可能导致出现意想不到的行为,例如您所看到的行为。

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

相关问题