完全自定义对话框简单实现


#完全自定义对话框

##布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#11ffffff">
    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:orientation="vertical">
        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:text="警告"

            android:textColor="#38ADFF"
            android:textSize="16sp"/>
        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="20dp"
            android:layout_gravity="center"
            android:text="保护视力,少玩手机"/>
        <View
            android:layout_width="match_parent"
            android:layout_height="1px"
            android:layout_marginTop="15dp"
            android:background="#E4E4E4"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:orientation="horizontal">
            <Button
                android:id="@+id/no"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginLeft="10dp"
                android:background="@null"
                android:gravity="center"
                android:lines="1"
                android:text="取消"
                android:textColor="#7D7D7D"
                android:textSize="16sp"/>
            <View
                android:layout_width="1px"
                android:layout_height="match_parent"
                android:background="#E4E4E4"/>
            <Button
                android:id="@+id/yes"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:layout_marginRight="10dp"
                android:background="@null"
                android:gravity="center"
                android:lines="1"
                android:text="确定"
                android:textColor="#38ADFF"
                android:textSize="16sp"/>
        </LinearLayout>
    </LinearLayout>
</RelativeLayout>

##继承的类

public class Mydialog extends Dialog {

    private TextView title;
    private TextView message;
    private Button no;
    private Button yes;



    //过度用的变量
    private String nostr;
    private String yestr;
    public void setNostr(String nostr) {
        this.nostr = nostr;
    }
    public void setYestr(String yestr) {
        this.yestr = yestr;
    }

    private yesNoOnclickListener yesNoOnclickListener;
    private noNoOnclickListener noNoOnclickListener;

    //创建一个自己定义的事件
    public interface yesNoOnclickListener{
        void yesOnclik();
    }
    public interface noNoOnclickListener{
        void noOnclik();
    }
    //set方法
    public void setYesNoOnclickListener(Mydialog.yesNoOnclickListener yesNoOnclickListener) {
        this.yesNoOnclickListener = yesNoOnclickListener;
    }
    public void setNoNoOnclickListener(Mydialog.noNoOnclickListener noNoOnclickListener) {
        this.noNoOnclickListener = noNoOnclickListener;
    }

    //过度用的变量
    private String titlestr;
    public void setTitlestr(String titlestr) {
        this.titlestr = titlestr;
    }
    //过度用的变量
    private String messagestr;
    public void setMessagestr(String messagestr) {
        this.messagestr = messagestr;
    }

    public Mydialog(@NonNull Context context) {
        super(context);
    }

    //创建一个该有的那个调用布局那个
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_layout);


        title = (TextView) findViewById(R.id.title);
        message = (TextView) findViewById(R.id.message);
        no = (Button) findViewById(R.id.no);
        yes = (Button) findViewById(R.id.yes);
        if(titlestr != null){
            title.setText(titlestr);
        }
        if(messagestr != null){
            message.setText(messagestr);
        }
        if(nostr != null){
            no.setText(nostr);
        }
        if(yestr != null){
            yes.setText(yestr);
        }

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(yesNoOnclickListener != null){
                    yesNoOnclickListener.yesOnclik();
                }
            }
        });

        no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(noNoOnclickListener != null){
                    noNoOnclickListener.noOnclik();
                }
            }
        });

    }
}

##调用和使用


 //完全自定义对话框
        text88.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final Mydialog mydialog = new Mydialog(MainActivity.this);
                //有了set方法就可以调用了
                mydialog.setTitlestr("我是头儿");
                mydialog.setMessagestr("请扇自己大嘴巴子!");
                mydialog.setNostr("否");
                mydialog.setYestr("是");

                //设置确定的方法
                mydialog.setYesNoOnclickListener(new Mydialog.yesNoOnclickListener() {
                    @Override
                    public void yesOnclik() {
                        Toast.makeText(MainActivity.this, "点击了是", Toast.LENGTH_SHORT).show();
                        mydialog.dismiss();
                    }
                });
                //设置取消的回调方法
                mydialog.setNoNoOnclickListener(new Mydialog.noNoOnclickListener() {
                    @Override
                    public void noOnclik() {
                        Toast.makeText(MainActivity.this, "点击了否", Toast.LENGTH_SHORT).show();
                        mydialog.dismiss();
                    }
                });
                //显示
                mydialog.show();
            }
        });

#系统菜单

为控件添加长按属性并将菜单绑定到这个控件上

    registerForContextMenu(heelo);
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item android:id="@+id/red" android:title="红色" android:icon="@mipmap/ic_launcher" app:showAsAction="ifRoom"></item>
    <item android:id="@+id/green" android:title="绿色"></item>
    <item android:id="@+id/blue" android:title="蓝色"></item>
</menu> 
 //创建系统菜单的方法
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        //调用方法
        getMenuInflater().inflate(R.menu.menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    //触发系统菜单的事件
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                Toast.makeText(this,item.getTitle() , Toast.LENGTH_SHORT).show();
                break;
            case R.id.green:
                Toast.makeText(this,item.getTitle() , Toast.LENGTH_SHORT).show();
                break;
            case R.id.blue:
                Toast.makeText(this,item.getTitle() , Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

#上下文菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item android:id="@+id/red" android:title="红色" android:icon="@mipmap/ic_launcher" app:showAsAction="ifRoom"></item>
    <item android:id="@+id/green" android:title="绿色"></item>
    <item android:id="@+id/blue" android:title="蓝色"></item>
</menu> 
 //创建上下文菜单的方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        getMenuInflater().inflate(R.menu.menu, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
    }
    //触发上下文菜单事件方法
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        int itemId = item.getItemId();
        switch (itemId){
            case R.id.red:
                Toast.makeText(this,item.getTitle() , Toast.LENGTH_SHORT).show();
                break;
            case R.id.green:
                Toast.makeText(this,item.getTitle() , Toast.LENGTH_SHORT).show();
                break;
            case R.id.blue:
                Toast.makeText(this,item.getTitle() , Toast.LENGTH_SHORT).show();
                break;
        }
        return super.onContextItemSelected(item);
    }

#弹出菜单

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    >
    <item android:id="@+id/red" android:title="红色" android:icon="@mipmap/ic_launcher" app:showAsAction="ifRoom"></item>
    <item android:id="@+id/green" android:title="绿色"></item>
    <item android:id="@+id/blue" android:title="蓝色"></item>
</menu> 
 text11 = (TextView) findViewById(R.id.text11);

        text11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //创建一个弹出窗口
                PopupMenu popupMenu = new PopupMenu(MainActivity.this, text11);
                //加载一下布局
                popupMenu.inflate(R.menu.menu);
                //事件监听给字体加颜色
                popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        int itemId = item.getItemId();
                        switch (itemId){
                            case R.id.red:
                                text11.setTextColor(Color.RED);
                                break;
                            case R.id.green:
                                text11.setTextColor(Color.GREEN);
                                break;
                            case R.id.blue:
                                text11.setTextColor(Color.BLUE);
                                break;
                        }
                        return false;
                    }
                });
                popupMenu.show();
            }
        });

#简单PopupWindow
##第一个布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".app2"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:background="#C0CCCC"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        >
        <TextView
            android:gravity="center_vertical"
            android:textColor="#585656"
            android:textSize="20sp"
            android:layout_marginLeft="30dp"
            android:text="微信"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />

        <ImageView
            android:id="@+id/add"
            android:layout_alignParentRight="true"
            android:layout_marginRight="30dp"
            android:layout_marginTop="11dp"
            android:src="@drawable/ic_launcher_background"
            android:layout_width="30dp"
            android:layout_height="30dp" />
    </RelativeLayout>


</LinearLayout>

##第二个布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:background="#45494A"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:textColor="#fff"
            android:textSize="20sp"
            android:text="发起群聊"
            android:gravity="center"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#fff">

    </View>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:textColor="#fff"
            android:textSize="20sp"
            android:gravity="center"
            android:text="发起尬聊"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="5dp"
        android:background="#fff">
    </View>
</LinearLayout>

##实现代码

 //初始化数据
        add = (ImageView) findViewById(R.id.add);
        //触发事件
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //创建对象
                PopupWindow popupWindow = new PopupWindow(app2.this);
                //加载布局
                View inflate = LayoutInflater.from(app2.this).inflate(R.layout.waibu_layout, null);
                //设置三要素缺一不可  布局 宽 高
                popupWindow.setContentView(inflate);
                popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);//包裹住
                popupWindow.setWidth(600);
                //设置外部点击取消
                popupWindow.setOutsideTouchable(true);
                //设置弹出的位置
                popupWindow.showAsDropDown(add,100,100);
            }
        });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值