android DialogFragment简单应用

阅读更多
一个简单的DialogFragment应用
因为Activity的showDialog(int id)被弃用了,要使用DatePickerDialog,TimePickerDialog,AlterDialog都要继承DialogFragment.
布局文件就一个Button,就不写出来了.

MainActivity.java
package sky.dialog;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button bt = (Button) findViewById(R.id.showDialog);
        bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showDialog();
			}
		});
    }
    public void showDialog()
    {
    	//根据传进的参数来实例化DialogFragment.
    	MyDialogFragment newDialog = MyDialogFragment.newInstance(MyDialogFragment.ALTER_DIALOG);
//    	MyDialogFragment newDialog = MyDialogFragment.newInstance(MyDialogFragment.DATE_PICKER_DIALOG);
//    	MyDialogFragment newDialog = MyDialogFragment.newInstance(MyDialogFragment.TIME_PICKER_DiALOG);
    	newDialog.show(getFragmentManager(), "alert msg");
    }
}


MyDialogFragment.java
package sky.dialog;

import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.os.Bundle;
import android.widget.DatePicker;
import android.widget.TimePicker;

public class MyDialogFragment extends DialogFragment {
	public static final int DATE_PICKER_DIALOG = 1;
	public static final int ALTER_DIALOG = 2;
	public static final int TIME_PICKER_DiALOG = 3;

	public static MyDialogFragment newInstance(int title) {
		MyDialogFragment myDialogFragment = new MyDialogFragment();
		Bundle bundle = new Bundle();
		bundle.putInt("title", title);
		myDialogFragment.setArguments(bundle);
		return myDialogFragment;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		int args = getArguments().getInt("title");
		//根据传进来的参数选择创建哪种Dialog
		switch (args) {
		case DATE_PICKER_DIALOG:
			return new DatePickerDialog(getActivity(), new OnDateSetListener() {
				public void onDateSet(DatePicker view, int year, int monthOfYear,
						int dayOfMonth) {
					// TODO Auto-generated method stub
					//月是从0开始的
					System.out.println( "year-->" + year + "  month-->" + monthOfYear
							+ "  day-->" + dayOfMonth);
				}
			}, 2012,3, 26);
		case ALTER_DIALOG:
			return new AlertDialog.Builder(getActivity())
				.setIcon(R.drawable.ic_launcher)
				.setTitle(getTag())
				.setPositiveButton("ok",
						new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					//点击ok触发的事件
					System.out.println("click ok!");
				}
				})
			.setNegativeButton("cancle",
					new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int whichButton) {
					//点击cancle触发的时间
					System.out.println("click cancle");
				}
				})
				.create();
		case TIME_PICKER_DiALOG:
			return new TimePickerDialog(getActivity(),new OnTimeSetListener() {
				
				public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
					// TODO Auto-generated method stub
					System.out.println("hour-->"+hourOfDay+"  minute-->"+ minute);
				}
			}, 13, 23, true);
		}
		return null;
	}

}



工程下载
  • DialogFragmentTest.tar.gz (133.1 KB)
  • 下载次数: 180

你可能感兴趣的:(android,dialogfragment,activity)