微信项目实战(二)

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中,重写菜单项

在这里插入图片描述

  1. LayoutInflater类型的变量inflater
    private LayoutInflater inflater;//布局生成器
  1. 重写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();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值