使用菜单触发:
switch (id){
case R.id.action0:
Log.i(TAG,"Action0");
FragmentTransaction ftrn = getFragmentManager().beginTransaction();
AlertDialog ad = new AlertDialog().newInstense("Alert信息:","恭喜您中奖了!",1);
ad.show(ftrn,TAG);
break;
case R.id.action1:
Log.i(TAG,"Action1");
FragmentTransaction ft = getFragmentManager().beginTransaction();
PromptDialog pd = new PromptDialog().newInstense("请输入新的手机号码:","例如:13300018888");
pd.show(ft,TAG);
break;
case R.id.action2:
Log.i(TAG, "Action2");
Toast.makeText(MainActivity.this,"Toast信息框",Toast.LENGTH_LONG).show();
break;
default:
Log.i(TAG,"Nothing");
break;
}
AlertDialog.class源代码:
package com.lanxin.dialogtest;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
/**
* Created by Administrator on 2016/5/4 0004.
*/
public class AlertDialog extends DialogFragment implements DialogInterface.OnClickListener {
private static final String TAG = "AlertDialog";
private static int butType = 0;
/**
* 信息框初始化
* @param title 信息标题
* @param msg 信息内容
* @param but 按钮 0、2:OK NO;1:OK
* @return
*/
public static AlertDialog newInstense(String title,String msg,int but){
AlertDialog adf = new AlertDialog();
Bundle bundle = new Bundle();
bundle.putString("message",msg);//映射消息
bundle.putString("title",title);//映射消息标题
adf.setArguments(bundle);//设置消息
butType = but;
return adf;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setCancelable(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
android.app.AlertDialog.Builder adb = new android.app.AlertDialog.Builder(getActivity())
.setTitle(this.getArguments().getString("title"));
if(butType == 0 || butType == 2){
adb.setPositiveButton(R.string.ok,this).setNegativeButton(R.string.no, this);
}else{
adb.setPositiveButton(R.string.ok,this);
}
adb.setMessage(this.getArguments().getString("message"));
return adb.create();
}
/**
*
* @param dialog
* @param which -1:OK按钮,-2:取消按钮
*/
@Override
public void onClick(DialogInterface dialog, int which) {
Log.i(TAG,"点击了我"+which);
dialog.dismiss();
}
}
PromptDialog.class源代码:
package com.lanxin.dialogtest;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by Administrator on 2016/5/4 0004.
*/
public class PromptDialog extends DialogFragment implements View.OnClickListener {
private static final String TAG = "PromptDialog";
private EditText editText;
public static PromptDialog newInstense(String title,String msg){
PromptDialog pdl = new PromptDialog();
Bundle bundle = new Bundle();
bundle.putString("message", msg);//映射消息
bundle.putString("title", title);//映射消息标题
pdl.setArguments(bundle);//设置消息
return pdl;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setCancelable(true);
int style = DialogFragment.STYLE_NORMAL, theme = 0;
setStyle(style, theme);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle)
{
View v = inflater.inflate(R.layout.prompt,container,false);
TextView tv = (TextView)v.findViewById(R.id.prompt_text);
String title = this.getArguments().getString("title");
if(!title.equals("")){
tv.setText(title);
}
editText = (EditText)v.findViewById(R.id.prompt_edit);
Button but_ok = (Button)v.findViewById(R.id.btn_ok);
Button but_no = (Button)v.findViewById(R.id.btn_no);
but_ok.setOnClickListener(this);
but_no.setOnClickListener(this);
if(icicle!=null){
editText.setText(icicle.getCharSequence("message"));
}
return v;
}
@Override
public void onSaveInstanceState(Bundle icicle) {
icicle.putCharSequence("message", editText.getText());
super.onPause();
}
@Override
public void onCancel(DialogInterface di) {
Log.v(TAG, "onCancel");
super.onCancel(di);
}
@Override
public void onDismiss(DialogInterface di) {
Log.v(TAG, "onDismiss");
super.onDismiss(di);
}
@Override
public void onClick(View v) {
int id = v.getId();
switch (id){
case R.id.btn_ok:
Log.i(TAG, "您输入:" + editText.getText());
break;
case R.id.btn_no:
Log.i(TAG,"你放弃输入");
break;
default:
break;
}
dismiss();
}
}
prompt.xml源代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:padding="4dip"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="@string/tip1"
android:layout_gravity="center_vertical|center_horizontal"
android:textAppearance="?android:textAppearanceMedium"
android:gravity="top|center_horizontal"
android:id="@+id/prompt_text"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/prompt_edit"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:textAppearanceMedium"/>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow>
<Button android:id="@+id/btn_no"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/no">
</Button>
<Button android:id="@+id/btn_ok"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/ok"
android:layout_weight="1">
</Button>
</TableRow>
</TableLayout>
</LinearLayout>
源代码下载:http://pan.baidu.com/s/1bp0p1L1

