对话框的实现
对话框.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:gravity="center"
android:background="@drawable/dialog_loading_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/id_recorder_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/recorder"
android:visibility="visible"
/>
<ImageView
android:id="@+id/id_recorder_voice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/v1"
android:visibility="visible"/>
</LinearLayout>
<TextView
android:id="@+id/id_recorder_text_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_recorder_up_cancel"
android:textColor="#ffffff"
android:layout_marginTop="5dp"
/>
</LinearLayout>
DialogManager.java
package com.sjy.myrecorder.view;
import java.security.KeyStore.PrivateKeyEntry;
import com.sjy.myrecorder.R;
import android.app.Dialog;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
public class DialogManager {
private Dialog mDialog;
private ImageView mRecorder;
private ImageView mVoice;
private TextView mLabel;
private Context mContext;
public DialogManager(Context context) {
mContext = context;
}
//显示对话框
public void showDialog() {
mDialog = new Dialog(mContext,R.style.Theme_AudioDialog);
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.dialog_recorder, null);
mDialog.setContentView(view);
mRecorder = (ImageView) mDialog.findViewById(R.id.id_recorder_icon);
mVoice = (ImageView) mDialog.findViewById(R.id.id_recorder_voice);
mLabel = (TextView) mDialog.findViewById(R.id.id_recorder_text_label);
mDialog.show();
}
//显示想要取消的对话框
public void wantToCancel() {
if(mDialog != null && mDialog.isShowing()) {
mRecorder.setVisibility(View.VISIBLE);
mVoice.setVisibility(View.GONE);
mLabel.setVisibility(View.VISIBLE);
mRecorder.setImageResource(R.drawable.cancel);
mLabel.setText(R.string.str_recorder_want_cancel);
}
}
//显示正在录音时的对话框
public void recording() {
if(mDialog != null && mDialog.isShowing()) {
mRecorder.setVisibility(View.VISIBLE);
mVoice.setVisibility(View.VISIBLE);
mLabel.setVisibility(View.VISIBLE);
mRecorder.setImageResource(R.drawable.recorder);
mLabel.setText(R.string.str_recorder_up_cancel);
}
}
//显示录音时间太短的对话框
public void tooShort() {
if(mDialog != null && mDialog.isShowing()) {
mRecorder.setVisibility(View.VISIBLE);
mVoice.setVisibility(View.GONE);
mLabel.setVisibility(View.VISIBLE);
mRecorder.setImageResource(R.drawable.voice_to_short);
mLabel.setText(R.string.str_recorder_up_cancel);
}
}
//对话框消失
public void dismissDialog() {
if(mDialog != null && mDialog.isShowing()) {
mDialog.dismiss();
mDialog = null;
}
}
//更新对话框中的声音级别
public void updateVoiceLevel(int level) {
if(mDialog != null && mDialog.isShowing()) {
//第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名。
int resId = mContext.getResources().getIdentifier("v" + level, "drawable", mContext.getPackageName());
mVoice.setImageResource(resId);
}
}
}