2

カスタム共有ダイアログを開くための非常に単純なアプリケーションを作成しています。XML レイアウトにはボタンが 1 つだけ含まれています。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white"
    android:gravity="center_horizontal">

    <Button android:layout_width="fill_parent" android:layout_height="wrap_content"
        android:layout_margin="20dip"
        android:text="Click here to open Share Dialog"
        android:onClick="onBtnShareClick"/>

</LinearLayout>

アクティビティでは、カスタム共有ダイアログを作成します

public class CustomDialog extends Activity {

    private static final int SHOW_DIALOG_SHARE = 1;
    private ArrayAdapter<ShareItem> mShareAdapter;

    @Override
    protected void onCreate(Bundle savedState) {
        super.onCreate(savedState);

        setContentView(R.layout.custom_dialog);

        final ShareItem[] items = {
            //new Item("Menu item", R.drawable.icon_assistance),
            new ShareItem("Banbe", R.drawable.ic_banbe),
            new ShareItem("Facebook", R.drawable.ic_facebook),
            new ShareItem("Twitter", R.drawable.ic_twitter),
            new ShareItem("Gmail", R.drawable.ic_gmail),
            new ShareItem("Other sharing options...", 0)
        };

        mShareAdapter = new ArrayAdapter<ShareItem>(
        this,
        android.R.layout.select_dialog_item,
        android.R.id.text1,
        items){
            public View getView(int position, View convertView, ViewGroup parent) {
                //User super class to create the View
                View v = super.getView(position, convertView, parent);
                TextView tv = (TextView)v.findViewById(android.R.id.text1);

                //Put the image on the TextView
                tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);

                //Add margin between image and text (support various screen densities)
                int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
                tv.setCompoundDrawablePadding(dp5);

                return v;
            }
        };
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case SHOW_DIALOG_SHARE:
            return new AlertDialog.Builder(this)
            .setIcon(R.drawable.icon)
            .setTitle(R.string.app_name)
            .setAdapter(mShareAdapter, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    Toast.makeText(CustomDialog.this, "Click on item " + item, Toast.LENGTH_SHORT).show();
                }
            })
            .show();
        }
        return null;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
    }

    public void onBtnShareClick(View v) {
        showDialog(SHOW_DIALOG_SHARE);
    }

    protected class ShareItem {
        public final String text;
        public final int icon;
        public ShareItem(String text, Integer icon) {
            this.text = text;
            this.icon = icon;
        }
        @Override
        public String toString() {
            return text;
        }
    }

}

ボタンをクリックすると、共有ダイアログが開きます。すべて良い。

ここで、デバイスを縦向きモードに回転させ、ボタンをクリックしてダイアログを開きます。その後、戻るを押して共有ダイアログを閉じます。デバイスを横向きに回転させます。ボタンをクリックしていないのに、突然共有ダイアログが再度開かれます。

ネイティブの共有ダイアログを使用しようとすると、このバグは見られません。カスタム共有ダイアログが原因でしょうか?

ここで何が問題なのか誰か教えてもらえますか?

4

4 に答える 4

4

こんにちは、アプリケーション マニフェスト ファイルに画面の向きのサポートを追加する必要があります。

 <activity android:name=".TestApp"
     android:label="@string/app_name"   android:configChanges="orientation">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

また、次のメソッドをオーバーライドします。

  public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
  }
于 2012-01-11T08:20:14.723 に答える
0

show()メソッドの代わりにcreate()メソッドを使用する

新しいAlertDialog.Builder(this).create()

于 2012-01-11T09:27:52.313 に答える