在 Activity 中显示对话框或弹出浮层时,尽量使用 DialogFragment,而非Dialog/AlertDialog,这样便于随Activity生命周期管理对话框/弹出浮层的生命周期。
AskDialogFragment.java
public class AskDialogFragment extends DialogFragment {
public Context mContext;
public LayoutInflater mInflater;
private View tipsView;
public TextView tipsTextView;
private TextView textTitle;
private Button buttonYes;
private Button buttonNo;
private AskFragmentInterface askFragmentInterface = null;
public static AskDialogFragment newInstance() {
Bundle args = new Bundle();
AskDialogFragment fragment = new AskDialogFragment();
fragment.setArguments(args);
return fragment;
}
public enum FOCUS_DIR {
FOCUS_YES, FOCUS_NO
}
@Override
public void onCreate(Bundle savedInstanceState) {
setStyle(AskDialogFragment.STYLE_NO_FRAME,R.style.Theme_No_Frame);
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
System.out.println("tag = "+getTag()); // tag which is from acitivity which started this fragment
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
this.mContext = getActivity();
this.mInflater = LayoutInflater.from(mContext);
initView();
initPopup();
setFocusDir(FOCUS_DIR.FOCUS_YES);
setButtonNoStatus(View.VISIBLE);
setTitle(R.string.warning);
setMessage(R.string.really_delete_all);
setPositiveButton(getString(R.string.yes), new View.OnClickListener() {
@Override
public void onClick(View view) {
askFragmentInterface.setAskPositiveClick();
dismiss();
}
});
setNegativeButton(getString(R.string.no), new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
buttonNo.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
buttonNo.clearFocus();
buttonYes.requestFocus();
buttonYes.setFocusable(true);
return true;
}
}
return false;
}
});
buttonYes.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_DPAD_LEFT:
case KeyEvent.KEYCODE_DPAD_RIGHT:
buttonYes.clearFocus();
buttonNo.requestFocus();
buttonNo.setFocusable(true);
return true;
}
}
return false;
}
});
return tipsView;
}
@Override
public void onResume() {
super.onResume();
}
public void setAskDialogListener(AskFragmentInterface listener){
askFragmentInterface = listener;
}
public interface AskFragmentInterface {
void setAskPositiveClick();
}
private void initView() {
// requestWindowFeature(Window.FEATURE_NO_TITLE);
tipsView = mInflater.inflate(R.layout.ask_tips_dialog, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
getActivity().getWindow().setWindowAnimations(R.style.popwin_anim_bottom_style);
textTitle = (TextView) tipsView.findViewById(R.id.asktipsTitleText);
tipsTextView = (TextView) tipsView.findViewById(R.id.asktips_dialog_tips_text);
buttonNo = (Button) tipsView.findViewById(R.id.asktips_dialog_butNo);
buttonYes = (Button) tipsView.findViewById(R.id.asktips_dialog_butYes);
}
public void setFocusDir(FOCUS_DIR dir) {
if (dir == FOCUS_DIR.FOCUS_NO) {
buttonYes.clearFocus();
buttonNo.requestFocus();
buttonNo.setFocusable(true);
} else {
buttonNo.clearFocus();
buttonYes.requestFocus();
buttonYes.setFocusable(true);
}
}
private void initPopup() {
// TODO Auto-generated method stub
WindowManager.LayoutParams lp = getActivity().getWindow().getAttributes();
lp.gravity = Gravity.CENTER;
lp.type = WindowManager.LayoutParams.TYPE_PHONE | WindowManager.LayoutParams.TYPE_SYSTEM_ERROR;
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
getActivity().getWindow().setAttributes(lp);
}
public void setMessage(CharSequence message) {
tipsTextView.setText(message);
}
public void setMessage(int messageId) {
tipsTextView.setText(messageId);
}
public void setTitle(CharSequence message) {
textTitle.setText(message);
}
public void setTitle(int messageId) {
textTitle.setText(messageId);
}
public void setPositiveButton(CharSequence text, Button.OnClickListener listener) {
buttonYes.setText(text);
buttonYes.setOnClickListener(listener);
}
public void setPositiveButton(int id, Button.OnClickListener listener) {
buttonYes.setText(id);
buttonYes.setOnClickListener(listener);
}
public void setNegativeButton(CharSequence text, Button.OnClickListener listener) {
buttonNo.setText(text);
buttonNo.setOnClickListener(listener);
}
public void setNegativeButton(int id, Button.OnClickListener listener) {
buttonNo.setText(id);
buttonNo.setOnClickListener(listener);
}
public void setButtonNoStatus(int visibility) {
buttonNo.setVisibility(visibility);
buttonYes.setFocusable(true);
buttonYes.requestFocus();
}
}
使用方法:
1.实现接口AskFragmentInterface
2.
final AskDialogFragment askTipsDialog = AskDialogFragment.newInstance();
askTipsDialog.setAskDialogListener(this);
askTipsDialog.show(getFragmentManager(), "dialog_fragment");
XML:
1.style
<style name="Theme_No_Frame" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">false</item>
<item name="android:windowIsTranslucent">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:backgroundDimEnabled">false</item>
<item name="android:backgroundDimAmount">1</item>
</style>
2.layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/transparent"
>
<LinearLayout
android:layout_width="460dp"
android:layout_height="260dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="@mipmap/add_panel"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/askTitle"
android:layout_width="match_parent"
android:layout_height="70dip"
android:gravity="center" >
<TextView
android:id="@+id/asktipsTitleText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="20dip"
android:layout_marginTop="15dip"
android:gravity="center"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="@color/text_color_black"
android:textSize="32sp" />
</LinearLayout>
<TextView
android:id="@+id/asktips_dialog_tips_text"
android:layout_width="wrap_content"
android:layout_height="80dip"
android:layout_gravity="center"
android:layout_marginLeft="40dip"
android:layout_marginRight="40dip"
android:layout_weight="1"
android:gravity="center"
android:textColor="@color/text_color_black"
android:textSize="26sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="56dip"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_marginBottom="8dip"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="@+id/asktips_dialog_butYes"
android:layout_width="200dip"
android:layout_height="match_parent"
android:layout_marginLeft="12dip"
android:paddingBottom="15dip"
android:background="@drawable/dialog_button_style"
android:textColor="@color/text_color_black"
android:textSize="24sp" />
<Button
android:id="@+id/asktips_dialog_butNo"
android:layout_width="200dip"
android:layout_height="match_parent"
android:background="@drawable/dialog_button_style"
android:textColor="@color/text_color_black"
android:paddingBottom="15dip"
android:layout_marginRight="12dip"
android:textSize="24sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>