Android:DialogFragmentの使用
9314 ワード
今日Androidドキュメントを見た時にDialogFragmentを見て興味を持って自分の理解に沿って例を作ってみたのですが、正しいのか正しくないのかはわかりませんが、普通に動いているのですが、その中で
バグも発見されましたが、上級者に解決してもらい、DialogFragmentもこのように使うべきではないかと共有しました.
1.一つ目は一般的な使い方
まずDialogFragmentクラスを書きます
そして呼び出し方法
2.2つ目はインタフェースを定義し、異なるトリガイベントを実現するDialogである.
まずクラスです.インタフェースが含まれています.
特定の呼び出しのActivityでNoticeDialogListenerを実装する必要がある
インタフェースの実装方法
よびだし
3.3つ目は、アプリケーションを実行する画面サイズに応じてDialog形式またはフルスクリーン形式で表示する
まずDialogFragmentクラスを定義します
ここでは、カスタムDialogFragmentのインタフェースレイアウトを追加し、トリガイベント、すなわち
purchase_items.xml
最後に呼び出し
ここがDialog形式で表示されているとき、後ろのボタンは依然として点をつけることができて、透明のようで、しかも点ごとに1つが表示されて上書きされて、私もどのように解決すべきか分かりません~
また、values、values-sw 600 dpなどのフォルダにboolsを追加する.xmlファイル、画面解像度を判断
最後に対応するソースコードであり、一般的なDialogの使用例もいくつか追加されています.
ソースのダウンロード
バグも発見されましたが、上級者に解決してもらい、DialogFragmentもこのように使うべきではないかと共有しました.
1.一つ目は一般的な使い方
まずDialogFragmentクラスを書きます
public static class FireMissilesDialogFragment extends DialogFragment {
/**
* Fragment
*
* @param title: 。
* @return:Fragment 。
*/
public static FireMissilesDialogFragment newInstance(String title) {
FireMissilesDialogFragment frag = new FireMissilesDialogFragment();
Bundle args = new Bundle();
//
args.putString("title", title);
frag.setArguments(args);
return frag;
}
/**
* Fragment onCreateDialog , DialogFragment show , 。
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// 。
String title = getArguments().getString("title");
// builder
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(title);
builder.setPositiveButton("fire", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
});
builder.setNegativeButton("cancle", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// dialog
return builder.create();
}
}
そして呼び出し方法
FireMissilesDialogFragment fire = FireMissilesDialogFragment.newInstance("fire_missiles?");
fire.show(getFragmentManager(), "dialog");
2.2つ目はインタフェースを定義し、異なるトリガイベントを実現するDialogである.
まずクラスです.インタフェースが含まれています.
public class NoticeDialogFragment extends DialogFragment {
//
NoticeDialogListener mListener;
/**
*
*/
public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}
/**
* Fragment.onAttach() NoticeDialogListener
*/
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activity
try {
// NoticeDialogListener , Activity
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// activity
throw new ClassCastException(activity.toString() + " must implement NoticeDialogListener");
}
}
/**
* Fragment onCreateDialog , DialogFragment show , 。
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// dialog button
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("fire_missiles").setPositiveButton("fire", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
}).setNegativeButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Send the negative button event back to the host activity
mListener.onDialogNegativeClick(NoticeDialogFragment.this);
}
});
return builder.create();
}
特定の呼び出しのActivityでNoticeDialogListenerを実装する必要がある
インタフェースの実装方法
/**
* ,
*/
@Override
public void onDialogPositiveClick(DialogFragment dialog) {
//
Toast.makeText(SecondActivity.this, "you chose fire", Toast.LENGTH_SHORT).show();
}
@Override
public void onDialogNegativeClick(DialogFragment dialog) {
//
Toast.makeText(SecondActivity.this, "you chose cancle", Toast.LENGTH_SHORT).show();
}
よびだし
// DialogFragment ,
NoticeDialogFragment dialog = new NoticeDialogFragment();
dialog.show(getFragmentManager(), "NoticeDialogFragment");
3.3つ目は、アプリケーションを実行する画面サイズに応じてDialog形式またはフルスクリーン形式で表示する
まずDialogFragmentクラスを定義します
public class CustomDialogFragment extends DialogFragment {
private Button cancleBtn;
private Button okBtn;
/**
* DialogFragment , Dialog , Fragment
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//
View view = inflater.inflate(R.layout.purchase_items, container, false);
cancleBtn = (Button) view.findViewById(R.id.cancle_btn);
okBtn = (Button) view.findViewById(R.id.ok_btn);
cancleBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
}
});
okBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getActivity(), "you chose ok!", Toast.LENGTH_SHORT).show();
dismiss();
}
});
return view;
}
/**
* The system calls this only when creating the layout in a dialog. Dialog
*/
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
/**
* onCreateView() Dialog 。 ,
* dialog , 。 , Dialog。
*/
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
}
ここでは、カスタムDialogFragmentのインタフェースレイアウトを追加し、トリガイベント、すなわち
purchase_items.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFBB33"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="this is the test"
android:textSize="20sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="@+id/cancle_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="cancle" />
<Button
android:id="@+id/ok_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="ok" />
</LinearLayout>
</RelativeLayout>
最後に呼び出し
FragmentManager fragmentManager = getFragmentManager();
CustomDialogFragment newFragment = new CustomDialogFragment();
if (mIsLargeLayout) {
// , Dialog
newFragment.show(fragmentManager, "dialog");
} else {
// ,
FragmentTransaction transaction = fragmentManager.beginTransaction();
//
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
// , “content” fragment , Activity
transaction.add(android.R.id.content, newFragment).addToBackStack(null).commit();
}
ここがDialog形式で表示されているとき、後ろのボタンは依然として点をつけることができて、透明のようで、しかも点ごとに1つが表示されて上書きされて、私もどのように解決すべきか分かりません~
また、values、values-sw 600 dpなどのフォルダにboolsを追加する.xmlファイル、画面解像度を判断
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="large_layout">false</bool>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="large_layout">true</bool>
</resources>
最後に対応するソースコードであり、一般的なDialogの使用例もいくつか追加されています.
ソースのダウンロード