0

我正在尝试开发一个在旋转时在其 TextViews 和 EditTexts 中保留值的应用程序。当未选择 EditText 时,我还想隐藏软键盘。但是,由于某种原因,当results可见时,该hideKeyboard()方法在多次旋转后崩溃。

以下是我的一些可能是罪魁祸首的方法:

/**
 * Detects if text boxes are not selected to hide keyboard
 * @param view - main view  
 */
private void setupUI(View view) {
   if(!(view instanceof EditText)) {
        view.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideKeyboard();
                return false;
            }
        });
    }
    if (view instanceof ViewGroup) {
        for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
            View innerView = ((ViewGroup) view).getChildAt(i);
            setupUI(innerView);
        }
    }
}

/**
 * Hides software keyboard
 */
private void hideKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) this.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0);             
}

/**
 * Saves instance state on rotation
 * @param outState - current state before rotation
 */
@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("loabox", loaBox.getText().toString());
    outState.putString("lwlbox", lwlBox.getText().toString());
    outState.putString("beambox", beamBox.getText().toString());
    outState.putString("displacementbox", displacementBox.getText().toString());
    outState.putString("sabox", saBox.getText().toString());

    if (results != null && !isEmpty(mhsText) && !isEmpty(dlText) && !isEmpty(sadText) && !isEmpty(capsizeText) && !isEmpty(comfortText)) {

        outState.putInt("results", results.getVisibility());

        outState.putString("mhs", mhsText.getText().toString());
        outState.putString("dl", dlText.getText().toString());
        outState.putString("sad", sadText.getText().toString());
        outState.putString("capsize", capsizeText.getText().toString());
        outState.putString("comfort", comfortText.getText().toString());

    }
    super.onSaveInstanceState(outState);
}

/**
 * Restores previous instance state after rotation
 * @param savedInstanceState - saved previous state before rotation
 */
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);

    if (savedInstanceState != null) {
        loaBox.setText(savedInstanceState.getString("loabox"));
        lwlBox.setText(savedInstanceState.getString("lwlbox"));
        beamBox.setText(savedInstanceState.getString("beambox"));
        displacementBox.setText(savedInstanceState.getString("displacementbox"));
        saBox.setText(savedInstanceState.getString("sabox"));

        mhsText.setText(savedInstanceState.getString("mhs"));
        dlText.setText(savedInstanceState.getString("dl"));
        sadText.setText(savedInstanceState.getString("sad"));
        capsizeText.setText(savedInstanceState.getString("capsize"));
        comfortText.setText(savedInstanceState.getString("comfort"));   

        results.setVisibility(savedInstanceState.getInt("results", results.getVisibility()));
    }
}

setupUI()方法在我的方法结束时调用onCreate()

有谁知道这里可能出了什么问题?

4

1 回答 1

0

请查看您的清单文件,您可以<activity>在该标签中找到您需要添加的标签android:windowSoftInputMode="stateAlwaysHidden"

于 2013-10-16T06:23:10.657 回答