文章目录
1.layout文件夹下,dialog_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33333333"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_item"
style="@style/dialog_item_text"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="1dp"/>
</LinearLayout>
2.layout文件夹下,dialog_cancel.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#33333333"
android:orientation="vertical">
<TextView
android:id="@+id/dialog_item"
style="@style/dialog_item_text"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="10dp"/>
</LinearLayout>
3.drawable文件夹下,dialog_item_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@color/dialog_item_press"
android:state_pressed="true"></item>
<item android:drawable="@color/dialog_item_normal"
android:state_pressed="false"></item>
</selector>
4.MainActivity中,重写菜单项
- LayoutInflater类型的变量inflater
private LayoutInflater inflater;//布局生成器
- 重写DialogAdapter extends BaseAdapter
/**
* 重写适配器
*/
private class DialogAdapter extends BaseAdapter {
@Override
public int getCount() {
return 3;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (position == 0 || position == 1) {
view = inflater.inflate(R.layout.dialog_item, parent, false);
} else {
view = inflater.inflate(R.layout.dialog_cancel, parent, false);
}
TextView tv = view.findViewById(R.id.dialog_item);
tv.setText(position == 0 ? "从相册选择" : position == 1 ? "拍照" : "取消");
return view;
}
}
3.addImg()函数
/**
* 函数名称:addImg()
* 函数功能:onClick方法,检查最大图片上传数
*
* @param view
*/
public void addImg(View view) {
if (count < 9) {
dialog.show();
} else {
Toast.makeText(this, "最多上传九张", Toast.LENGTH_LONG).show();
}
}
5.MainActivity源码
package edu.qau.phone;
import android.app.Activity;
import com.orhanobut.dialogplus.DialogPlus;
import com.orhanobut.dialogplus.OnItemClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private DialogPlus dialog;//对话框
private int count = 0;//图片数量
private ImageView image;//图片视图
/**
* 函数名称:onCreate
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init() {
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
DialogAdapter adapter = new DialogAdapter();
// 新建Dialog对话框
dialog = DialogPlus.newDialog(this).setAdapter(adapter)
.setContentBackgroundResource(R.color.dialog_bg)
.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(@NonNull DialogPlus dialog, @NonNull Object item,
@NonNull View view, int position) {
if (position == 1) {
dialog.dismiss();
take();//拍照
} else if (position == 0) {
pick();//相册
} else {
dialog.dismiss();
}
}
}).setExpanded(false).create();
}
/**
* 重写适配器
*/
private class DialogAdapter extends BaseAdapter {
@Override
public int getCount() {
return 3;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (position == 0 || position == 1) {
view = inflater.inflate(R.layout.dialog_item, parent, false);
} else {
view = inflater.inflate(R.layout.dialog_cancel, parent, false);
}
TextView tv = view.findViewById(R.id.dialog_item);
tv.setText(position == 0 ? "从相册选择" : position == 1 ? "拍照" : "取消");
return view;
}
}
/**
* 函数名称:addImg()
* 函数功能:onClick方法,检查最大图片上传数
* @param view
*/
public void addImg(View view) {
if (count < 9) {
dialog.show();
} else {
Toast.makeText(this, "最多上传九张", Toast.LENGTH_LONG).show();
}
}
}