EditText Focus()之后的onResume()问题

时间:2014-07-11 11:14:17

标签: android android-edittext focus onresume

我正在使用onResume()检查用户是否按了后退按钮,然后将用户带到某个fragment

如果在EditText上禁用了焦点,则工作正常。但是,如果焦点在EditText,那么如果用户按下后退按钮,它就完成了应用程序。

所以,我想解雇onResume()并根据代码将用户带到Home() Fragment,无论是否有关于EditText的焦点。

如何防止这个问题?

我的代码:

etComment.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stub

        etComment.setFocusable(true);
        etComment.setFocusableInTouchMode(true);

        return false;
    }
});

onResume():

@Override
public void onResume() {

    super.onResume();

    getView().setFocusable(true);
    getView().setFocusableInTouchMode(true);
    getView().requestFocus();
    getView().setOnKeyListener(new View.OnKeyListener() {

        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // TODO Auto-generated method stub

            if (event.getAction() == KeyEvent.ACTION_UP
                    && keyCode == KeyEvent.KEYCODE_BACK) {

                for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
                    fm.popBackStack();
                }

                fm.beginTransaction().add(R.id.frame, new Home()).commit();

                return true;
            }

            return false;
        }
    });
}

2 个答案:

答案 0 :(得分:1)

添加此代码

<div class="panel panel-default">
  <div class="panel-body hotel-panel">
    <div class="row">
      <div class="col-sm-4 pull-left panel-image">
        <img src="http://s3.amazonaws.com/governmentswagger/IMG_2843.JPG" alt="asdf" width="250">
      </div>
      <div class="col-sm-5">
        <h4>Hilton Atlanta Buckhead</h4>
        <p>Free parking</p>
        <p>Free wifi</p>
      </div>
      <div class="col-sm-3 rate text-center">
        <h4>$289</h4>
        <button type="button" class="btn btn-primary">Govt Rate Link</button>
      </div>
    </div>
  </div>
</div>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">

答案 1 :(得分:0)

我认为只使用onBackPressed() mPausingmBackPressed会更好。

private boolean mPausing = false;
private boolean mBackPressed = false;

@Override
protected void onPause() {
    super.onPause();
    mPausing = true;
}

@Override
protected void onResumeFragments() {
    super.onResumeFragments();
    mPausing = false;
    if (mBackPressed) {
        onBackPressed();
        mBackPressed = false;
    }
}

@Override
public void onBackPressed() {
    if (mPausing) {
        mBackPressed = true;
        return;
    }

    for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
        fm.popBackStack();
    }
    fm.beginTransaction().add(R.id.frame, new Home()).commit();
}

然后你不必担心焦点。是否有使用setFocusable()方法的原因?

此外,我正在使用onResumeFragments()代替onResume()Fragment Transactions & Activity State Loss

这篇文章说:

  

例如,您不应在FragmentActivity#onResume()方法中提交事务,因为在某些情况下可以在恢复活动状态之前调用该方法(有关详细信息,请参阅documentation) )。