1. 程式人生 > >DialogFragment的使用方法簡介

DialogFragment的使用方法簡介

DialogFragment在android 3.0時被引入。是一種特殊的Fragment,用於在Activity的內容之上展示一個模態的對話方塊。典型的用於:展示警告框,輸入框,確認框等等。


總結起來分為三個步驟:

 
 

1、建立DialogFragment   Layout佈局;

2、寫一個類繼承DialogFragment,完成相關程式碼;

3、在MainActivity中呼叫,顯示該DialogFragment ;

具體步驟:

1、建立DialogFragment   Layout佈局;

<style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    
  <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:gravity="center"
        android:text="註冊失敗"
        android:textColor="#000000"
        android:textSize="20sp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        android:gravity="center"
        android:text="手機已註冊"
        android:textColor="#c3c3c3"
        android:textSize="20sp" />
    <Button
        android:id="@+id/confirm"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:text="確定"
        android:textColor="#ff55aab7"
        android:textSize="20sp" />
</LinearLayout>


佈局:


2、寫一個類繼承DialogFragment;

import android.app.DialogFragment;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.WindowManager;

public class mDialogFragment extends DialogFragment {
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		//繪製DialogFragment的View,返回一個view物件
		View view = inflater.inflate(R.layout.dialogfrag, container, false);
		//DialogFragment設定為無標題
	    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
	    //DialogFragment設定為透明的
	    getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
	    //設定DialogFragment的位置
	    final WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
	    layoutParams.gravity = Gravity.BOTTOM;
	    getDialog().getWindow().setAttributes(layoutParams);

	  <span style="white-space:pre">	</span>//點選確認DialogFragment消失
		(view.findViewById(R.id.confirm)).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				dismiss();
			}
		});
		
		return view;
	
	}
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		//設定DialogFragment的大小,在onResume()中設定;
		getDialog().getWindow().setLayout(1080, 800);

	}

}

3、在MainActivity中呼叫,顯示該 DialogFragment ;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        //點選 DialogFragment
        findViewById(R.id.dialogfragment).setOnClickListener(new OnClickListener() {			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				//例項化DialogFragment
				final mDialogFragment mdialogFragment = new mDialogFragment();
				//show  dialogFragment
				mdialogFragment.show(getFragmentManager(), "confirmDialogFragment");
				//設定1.5秒後,dialogFragment消失
				new Handler().postDelayed(
						new Runnable() {

							@Override
							public void run() {
							mdialogFragment.dismiss();
							}
						}, 1500);	
			}
		});

    }

}
點選DialogFragment