2

我正在使用One Gulli Bulls 屏幕方向更改 - 保存焦点示例以在方向更改时重新获得焦点。

我的活动AddGearbox并打开一个对话框CustomizeDialog(带有 的活动theme.dialog)。

该对话框有一些文本框。在onSaveInstanceState我保存我需要的东西并且能够在restoreDialog. 但是焦点没有恢复。

我正在记录方向更改时具有焦点的视图的 ID,但我猜该视图是在显示对话框的活动中,而不是在对话框本身中?

该怎么办?

添加齿轮箱.java

public class AddGearbox extends FragmentActivity implements
        MyListFragment.MyOnClickSelectedListener, DialogInterface.OnDismissListener         {

    CustomizeDialog customizeDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
       customizeDialog = new CustomizeDialog(this);

    if(savedInstanceState!=null) {
        restoreDialog(savedInstanceState);
    }

    }
....
....
....
@Override
protected void onSaveInstanceState(Bundle saveState) {
    super.onSaveInstanceState(saveState);

    saveState.putBoolean("isDialogOpen",isDialogOpen);

    if (isDialogOpen) {

        // Save a lot of stuff

        View focusedChild = getCurrentFocus();

        if (focusedChild != null)
        {
           int focusID = focusedChild.getId();
           int cursorLoc = 0;

           if (focusedChild instanceof EditText)
           {
              cursorLoc = ((EditText) focusedChild).getSelectionStart();
           }

           saveState.putInt("focusID", focusID);
           saveState.putInt("cursorLoc", cursorLoc);
        }
    }

}


private void restoreDialog(Bundle savedInstanceState) {
    isDialogOpen=savedInstanceState.getBoolean("isDialogOpen");
    if (isDialogOpen) {
        customizeDialog = new CustomizeDialog(this);

        // Restore a lot of stuff

        int focusID = savedInstanceState.getInt("focusID", View.NO_ID);

        View focusedChild = findViewById(focusID);
        if (focusedChild != null) {
            focusedChild.requestFocus();

            if (focusedChild instanceof EditText) {
                int cursorLoc = savedInstanceState.getInt("cursorLoc", 0);
                ((EditText) focusedChild).setSelection(cursorLoc);
            }
        }

        customizeDialog.show();
    }

CustomizeDialog.java

public class CustomizeDialog extends Dialog implements OnClickListener {

public CustomizeDialog(AddGearbox mainActivity) {
       super(mainActivity); 
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.customizedialog);
4

0 回答 0