您的位置:首页 > 其它

AlertDialog弹出框-常用几种写法

2015-12-01 13:36 267 查看
常用方法一:普通写法

// AlertDialog不能new,没有构造器,但是里面的Builder内部类有new的方法
AlertDialog.Builder builder = new Builder(this);
// 设置图标,使用安卓自带的图标
builder.setIcon(android.R.drawable.alert_dark_frame);
// 设置标题
builder.setTitle("欲练此功必先自宫");
// 设置文本
builder.setMessage("李志平,想清楚哦");

// 设置确定按钮
builder.setPositiveButton("确定", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "感谢使用本软件,再见", 0).show();
}
});

// 设置取消按钮
builder.setNegativeButton("取消", new OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "若不自宫,一定不成功", 0).show();
}
});
// 使用创建器,生成一个对话框对象
AlertDialog ad = builder.create();
ad.show();


常用方法二:连贯写法

new AlertDialog.Builder(WeiListenRecordingStudioSoundEditActivity.this).setTitle("退出编辑").setMessage("退出编辑将会丢失编辑音乐,重新录音,确认退出?")
.setPositiveButton("离开编辑", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

}
})
.setNegativeButton("继续编辑", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

}
}) .show();
常用方法三:控制弹出框的显式和消失

private final static int SAVE_DIALOG_KEY = 0;

new ConvertAsyncTask().execute();

//转换成mp3
public class ConvertAsyncTask extends AsyncTask<String, Integer, Boolean> {
@Override
protected Boolean doInBackground(String... params) {
return isRaw2mp3();
}

@Override
protected void onPostExecute(Boolean result) {
removeDialog(SAVE_DIALOG_KEY);
if (result) {
LogUtils.v("file:", "::"+rawFile+"..."+mp3File);

isEdit = true;
tvSoundEditKey.setEnabled(false);
tvSoundEditKey.setImageResource(R.drawable.recording_studio_ben_cut);
iv_ripple.setVisibility(View.GONE);
studioEdit.setVisibility(View.VISIBLE);
llRecordTime.setVisibility(View.GONE);
llEditTime.setVisibility(View.VISIBLE);

startEdit();//开始剪切
}
}

@Override
protected void onPreExecute() {
showDialog(SAVE_DIALOG_KEY);
}
}

/* raw2mp3、raw或者pcm转换成mp3 - use flame */
    private boolean isRaw2mp3() {

        if (isRecording) {
            return false;
        }
        File file = new File(rawFile);
        if (!file.exists()) {
            return false;
        }

        FLameUtils lameUtils = new FLameUtils(1, frequence, 128);
        boolean isComplete = lameUtils.raw2mp3(rawFile, mp3File);// raw2mp3其中raw和pcm都可以转成mp3

        return isComplete;
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        ProgressDialog dialog = new ProgressDialog(WeiListenRecordingStudioSoundEditActivity.this);
        dialog.setCancelable(false);// 控制弹出框是否可点击
        switch (id) {
        case SAVE_DIALOG_KEY: {
            dialog.setMessage("保存中...");
            break;
        }
        }
        return dialog;
    }


常用方法四:单独写一个类来写dialog

Message message = Message.obtain(handler);
WeiListenRecordingStudioFileSaveDialog dlog = new WeiListenRecordingStudioFileSaveDialog(this, getResources(),
mTitle, message);
dlog.show();

========

</span><span style="font-family:Monaco;">类:</span>
public class WeiListenRecordingStudioFileSaveDialog extends Dialog {
public WeiListenRecordingStudioFileSaveDialog(Context context,
Resources resources,
String originalName,
Message response) {
super(context);

// Inflate our UI from its XML layout description.
setContentView(R.layout.wei_listen_recording_studio_file_save);
}}
常用方法五:从底部显式dialog动画

java:
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog dialog = new AlertDialog.Builder(TestAndroid1Activity.this)
.setTitle("title").setMessage("message").create();
Window window = dialog.getWindow();
window.setGravity(Gravity.BOTTOM);  //此处可以设置dialog显示的位置
window.setWindowAnimations(R.style.mystyle);  //添加动画
dialog.show();
}
});

========

</span>styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="mystyle" parent="android:Animation">
        <item name="@android:windowEnterAnimation">@anim/dialog_enter</item>  //进入时的动画
        <item name="@android:windowExitAnimation">@anim/dialog_exit</item>    //退出时的动画
    </style>
</resources>

========

</span>res/anim/dialog_enter.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    
    <translate
        android:fromYDelta="100%p"       %p指相对于父容器
        android:duration="600"
        />
</set>

=========

</span>res/anim/dialog_exit.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
     
      <translate
          android:toYDelta="100%p"
          android:duration="600"    //持续时间
          />
</set>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: