ActionBarSherlock动作条消失时,重点从EditText上更改按钮 [英] ActionBarSherlock actionbar disappears when focus changes from EditText to Button

查看:127
本文介绍了ActionBarSherlock动作条消失时,重点从EditText上更改按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含两个字段的EditText和一个按钮来登录一个简单的登录布局。问题是,在ActionBar消失时,软键盘是开放的,我将焦点从一个EditText的按钮,动作条再回来的时候我preSS回来。当软键盘都被关闭,不会发生的问题,我通过EditTexts和按键采用DPAD导航。

我用ActionBarSherlock,并且只发生在Android 2.x的模拟器的问题,在4.x的模拟器一切正常。我知道ActionBarSherlock使用在Android版本,它提供了原生的ActionBar实现,所以它可能是与ActionBarSherlock code的一个问题。

我还执行的测试,以检查 ActionBar.isShowing的值(),但是这回真,即使在动作条不显示在屏幕上。

我也弄不清是什么在这种情况下怎么回事,有人有一些想法?

布局XML

 < XML版本=1.0编码=UTF-8&GT?;
< RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    工具:忽略=UselessParent>

    <的LinearLayout
        机器人:layout_width =250dp
        机器人:layout_height =WRAP_CONTENT
        机器人:layout_centerInParent =真
        机器人:方向=垂直>

        <的EditText
            机器人:ID =@ + ID /用户名
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginBottom =10dp
            机器人:提示=@字符串/用户名
            机器人:inputType =textNoSuggestions
            机器人:nextFocusUp =@ + ID / loginButton
            机器人:imeOptions =actionNext/>

        <的EditText
            机器人:ID =@ + ID /密码
            机器人:layout_width =FILL_PARENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_marginBottom =10dp
            机器人:提示=@字符串/密码
            机器人:inputType =textPassword
            机器人:imeOptions =actionDone/>

        <按钮
            机器人:ID =@ + ID / loginButton
            机器人:layout_width =WRAP_CONTENT
            机器人:layout_height =WRAP_CONTENT
            机器人:layout_gravity =右
            机器人:文本=@字符串/登录
            机器人:TEXTSIZE =20SP
            机器人:nextFocusDown =@ + ID /用户名/>

    < / LinearLayout中>

< / RelativeLayout的>
 

片段code

 公共类LoginFragment扩展BaseFragment {

    @InjectView(R.id.loginButton)保护按钮mLoginButton;
    @InjectView(R.id.username)保护的EditText mUsernameEditText;
    @InjectView(R.id.password)保护的EditText mPasswordEditText;

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
        返回inflater.inflate(R.layout.login,集装箱,假);
    }

    @覆盖
    公共无效的OnStart(){
        super.onStart();

        mLoginButton.setEnabled(allFieldsValid());
        mLoginButton.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                handleLogin();
            }
        });

        mPasswordEditText.setOnEditorActionListener(新TextView.OnEditorActionListener(){

            @覆盖
            公共布尔onEditorAction(TextView的V,INT actionId,KeyEvent的事件){
                如果(actionId == EditorInfo.IME_ACTION_DONE&安培;&安培; allFieldsValid()){
                    handleLogin();
                }
                返回false;
            }
        });

        TextWatcher fieldValidatorTextWatcher =新TextWatcher(){
            @覆盖
            公共无效afterTextChanged(编辑S){
            }

            @覆盖
            公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,之后INT){
            }

            @覆盖
            公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,诠释计数){
                mLoginButton.setEnabled(allFieldsValid());
            }
        };

        mUsernameEditText.addTextChangedListener(fieldValidatorTextWatcher);
        mPasswordEditText.addTextChangedListener(fieldValidatorTextWatcher);
    }

    私人无效handleLogin(){
        。getSherlockActivity()setSupportProgressBarIndeterminateVisibility(真正的);

        意向意图=新的意图(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(意向);
    }

    私人布尔allFieldsValid(){
        返回usernameFieldIsValid()&安培;&安培; passwordFieldIsValid();
    }

    私人布尔usernameFieldIsValid(){
        返回TextUtils.isEmpty(mUsernameEditText.getText())!;
    }

    私人布尔passwordFieldIsValid(){
        返回TextUtils.isEmpty(mPasswordEditText.getText())!;
    }

    @覆盖
    公共无效onReceiveResult(INT结果code,捆绑resultData){
        。getSherlockActivity()setSupportProgressBarIndeterminateVisibility(假);
        super.onReceiveResult(结果code,resultData);
    }

    @覆盖
    公共无效onReceiveResultSuccess(包resultData){
        ((LoginActivity)getActivity())redirectToSelectTeamwebActivity(resultData.getInt(LoginService.RESULT_USER_ID))。
    }

    @覆盖
    公共无效onReceiveResultFailure(包resultData){
        mPasswordEditText.setText();
        字符串的errorMessage =的getString(R.string.invalid_login_credentials);
        Toast.makeText(getActivity(),的errorMessage,Toast.LENGTH_LONG).show();
    }

}
 

解决方案

ActionBarSherlock重视里面的内容来看,而不是在窗口的装饰看法pre-ICS兼容操作栏。正因为如此,很容易受到更多的不便,这可能会导致像你看到一个意外的行为。

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

I have a simple login layout that contains two EditText fields and a Button to login. The problem is that the ActionBar disappears when the soft keyboard is open and I change the focus from an EditText to the Button, and the ActionBar comes back again when I press back. The problem does not occur when the soft keyboard is closed and I navigate through the EditTexts and Button with the DPAD.

I use ActionBarSherlock, and the problem occurs only on Android 2.x emulators, on the 4.x emulators everything is normal. I know ActionBarSherlock uses the native ActionBar implementations on Android versions where it is available, so it probably is a problem with the ActionBarSherlock code.

I also executed a test to check the value of ActionBar.isShowing(), but this returned true even when the ActionBar was not visible on the screen.

I cannot figure out what is going on in this case, anybody has some ideas?

Layout 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>

FRAGMENT 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();
    }

}

解决方案

ActionBarSherlock attaches a compatibility action bar on pre-ICS inside of the content view rather than in the Window's decor view. Because of this, it is susceptible to more inconveniences which may cause unexpected behavior like the one you are seeing.

If you have set the windowSoftInputMode to pan the content view when the IME is open the action bar will disappear from the screen. The simple way around this would be to use a different mode (e.g., resize) which will re-layout the content view and keep the action bar on the screen.

这篇关于ActionBarSherlock动作条消失时,重点从EditText上更改按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆