1

我必须开发一个 android 应用程序。

我创建了一个警报对话框。如果我必须旋转方向意味着警报对话框消失。

但是我希望在方向改变时显示警报对话框。

@Override
   public void onConfigurationChanged ( Configuration newConfig )
  {
      super.onConfigurationChanged(newConfig);
    try
    {
        MainActivity.editCalled = true;
        Intent in = new Intent(AndroidListFragmentActivity.this, AndroidListFragmentActivity.class);
        startActivity(in);
        finish();
    }
     catch (Exception e)
    {
        e.printStackTrace();
    }
}

在这里我使用了两个片段......

在具有一个警报对话框的第二个片段中:

    ImageView share = (ImageView) findViewById(R.id.imageView5);
    share.setOnClickListener(new OnClickListener()
      {
        public void onClick ( View v )
        {
            final CharSequence[] items =
            {
                    "Facebook", "Twitter", "Email"
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(SubCate.this);
            builder.setTitle("Share Via:");
            builder.setItems(items, new DialogInterface.OnClickListener()
            {
                public void onClick ( DialogInterface dialog , int item )
                {
                    if (items[item] == "Facebook")
                    {

                        onFacebookClick();
                    }
                    if(items[item] == "Twitter"){

                        onClickTwitt();
                       } 
                    if (items[item] == "Email")
                    {
                        Intent email = new Intent(Intent.ACTION_SEND);
                        email.setType("message/rfc822");

                        email.putExtra(Intent.EXTRA_EMAIL, new String[]
                        {
                                ""
                        });
                        email.putExtra(Intent.EXTRA_SUBJECT, _Substring);
                        email.putExtra(Intent.EXTRA_TEXT, ContentEmail);
                        startActivity(Intent.createChooser(email, "Choose an Email client :"));

                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
      });
}
Here only i  have facing above problem..please give me solution for these ???
4

2 回答 2

6

android:configChanges="orientation"Android不鼓励设置。您可以首先Alertdialog在您的片段中声明,然后使用onSavedInstanceState

AlertDialog alert;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.yourid, container, false);

        if(savedInstanceState != null && savedInstanceState.getBoolean("alertShown",true)) {
            showDialog(view.getContext());
        }
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    if(alert != null && alert.isShowing()) {
        // close dialog to prevent leaked window
        alert.dismiss();
        outState.putBoolean("alertShown", true);
    }
}

// put all creating dialog stuff in a single method
protected void showDialog(final Context context) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    ...
    alert = builder.create();
    alert.show();
}
于 2013-05-16T07:27:48.893 回答
1

正如这里很多人建议的那样,

android:configChanges="keyboardHidden|orientation"

不是解决方案。充其量是一个黑客。处理此问题的正确方法是通过您的活动管理对话。您需要在活动代码中覆盖一些方法,如下所示:

protected Dialog onCreateDialog(int id) {
    // create and return your dialog instance here
    AlertDialog dialog = new AlertDialog.Builder(context)
        .setTitle(title)
        .setIcon(R.drawable.indicator_input_error)
        .setMessage(message)
        .create();
    dialog.setButton(
            DialogInterface.BUTTON_POSITIVE,    
            context.getString(R.string.OK),
            (DialogInterface.OnClickListener) null);
    return dialog;
}

protected void onPrepareDialog(int id, Dialog dialog) {
    // You dialog initialization code here
}

在你完成这个之后。您使用以下方式显示对话框:

showDialog(yourDialogID);

完成此操作后,您将看到如果发生配置更改,您的对话框也将重新创建。最好的部分是您的活动将为您管理对话。如果您执行大量初始化,它将尽可能重用,从而减少对话框加载时间。

于 2013-05-16T06:57:13.023 回答