Android API指南(二)Dialog篇

本文介绍Dialog。主要包括:DialogFragment的基本用法,以及自定义对话样式的方法。

Dialog的基本用法

建议:使用DialogFragment作为对话框的容器。DialogFragment类提供了创建对话框并管理其外观需要的所有控件,而不再需要调用Dialog的方法。

下面介绍DialogFragment的基本使用方法。

点击查看:Dialog的基本用法完整代码

1. 创建DialogFragment

public class DialogA extends DialogFragment implements DialogInterface.OnClickListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage(R.string.dialoga_title)
               .setPositiveButton(R.string.ok, this)
               .setNegativeButton(R.string.cancel, this);

        return builder.create();
    }   

    @Override
    public void onClick(DialogInterface dialog, int id) {
        switch(id) {
            case AlertDialog.BUTTON_NEGATIVE:
                Toast.makeText(getActivity(), "Negative", Toast.LENGTH_SHORT).show();
                break;
            case AlertDialog.BUTTON_POSITIVE:
                Toast.makeText(getActivity(), "Positive", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
       }   
   }   
}

说明:自定义一个DialogFragment,并重写它的onCreateDialog()方法。

2. 调用该DialogFragment

下面是在FragmentActivity中调用该DialogFragment对话框。

public class DialogTest extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        showDialog();
    }

    private void showDialog() {
        FragmentManager fm = getSupportFragmentManager();
        DialogA dialoga = new DialogA();
        dialoga.show(fm, "fragmenta");
    }
}

自定义DialogFragment布局

下面介绍自定义DialogFragment的布局的方法

点击查看:自定义DialogFragment布局的完整代码

1. 设置布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/dialoga_intro" />

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_action_video" />

</LinearLayout>

2. 使用布局

public class DialogA extends DialogFragment implements DialogInterface.OnClickListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.dialoga, null))
               .setMessage(R.string.dialoga_title)
               .setPositiveButton(R.string.ok, this)
               .setNegativeButton(R.string.cancel, this);

        return builder.create();
    }

    @Override
    public void onClick(DialogInterface dialog, int id) {
        switch(id) {
            case AlertDialog.BUTTON_NEGATIVE:
                Toast.makeText(getActivity(), "Negative", Toast.LENGTH_SHORT).show();
                break;
            case AlertDialog.BUTTON_POSITIVE:
                Toast.makeText(getActivity(), "Positive", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
       }
   }
}

DialogFragment和Activity的交互

下面介绍自定义DialogFragment和Activity交互的方法

点击查看:DialogFragment和Activity交互的完整代码

1. 定义通信接口

在DialogFragment中定义它们之间的通信接口。

public interface NoticeDialogListener {
    public void onDialogPositiveClick(DialogFragment dialog);
    public void onDialogNegativeClick(DialogFragment dialog);
}   

// Use this instance of the interface to deliver action events
NoticeDialogListener mListener;

// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    // Verify that the host activity implements the callback interface
    try {
        // Instantiate the NoticeDialogListener so we can send events to the host
        mListener = (NoticeDialogListener) activity;
    } catch (ClassCastException e) {
        // The activity doesn't implement the interface, throw exception
        throw new ClassCastException(activity.toString()
                + " must implement NoticeDialogListener");
    }   
}  

2. 在DialogFragment中调用该接口

@Override
public void onClick(DialogInterface dialog, int id) {
    switch(id) {
        case AlertDialog.BUTTON_POSITIVE:
            //Toast.makeText(getActivity(), "Negative", Toast.LENGTH_SHORT).show();
            mListener.onDialogPositiveClick(DialogA.this);
            break;
        case AlertDialog.BUTTON_NEGATIVE:
            //Toast.makeText(getActivity(), "Positive", Toast.LENGTH_SHORT).show();
            mListener.onDialogNegativeClick(DialogA.this);
            break;
        default:
            break;
   }   

}

3. 在Activity中实现该接口

public class DialogTest extends FragmentActivity 
    implements DialogA.NoticeDialogListener {

    ...

    @Override
    public void onDialogPositiveClick(DialogFragment dialog) {
        Toast.makeText(this, "Positive Callback", Toast.LENGTH_SHORT).show();
    }
    @Override
    public void onDialogNegativeClick(DialogFragment dialog) {
        Toast.makeText(this, "Negative Callback", Toast.LENGTH_SHORT).show();
    }
}
by skywang
Previous     Next