Toast,蒙版,AlertDialog(api的使用)(style的操作)

君子自强不息

蒙版,主要是对checkBox和布局的使用,在布局中单独的添加一张北京为模糊的图片作为蒙版,是一幅图片覆盖在另一个图片的正上方。

使用时注意把position设置为final
以下具体操作代码的部分,首先建立一个数组存放CheckBox的状态,用数组中的值去填写没一个状态。

    //得到布局中的checkBox
     viewHolder.checkBox= (CheckBox) convertView.findViewById(R.id.checkbox);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        Fruit fruit=mList.get(position);
        viewHolder.imageView.setImageResource(fruit.getImage());
        viewHolder.textView.setText(fruit.getName());
        //设置选中事件
        viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mCheckBox[position] = isChecked;
                //修改后立即更新
                notifyDataSetChanged();
            }
        });
        viewHolder.checkBox.setChecked(mCheckBox[position]);
        if(mCheckBox[position]){
            //通过checkbox的状态来改变蒙版是否显示
            viewHolder.image_tint.setVisibility(View.VISIBLE);
        }else {
            viewHolder.image_tint.setVisibility(View.INVISIBLE);
        }
        return convertView;
    }
    class ViewHolder{
        ImageView image_tint;
        CheckBox checkBox;
        TextView textView;
        ImageView imageView;
    }

最终结果

这里写图片描述

Toast查看api

使用富文本的Toast(富文本参考前面的View博客)

Toast toast = Toast.makeText(getApplicationContext(), "zheshi   ", Toast.LENGTH_SHORT);

            Spanned span = Html.fromHtml("我是<img src=''/>一个<font color='#ff0000'>Toast</font>", new Html.ImageGetter() {

                @Override
                public Drawable getDrawable(String source) {
                    Drawable drawale = getResources().getDrawable(R.drawable.ic_launcher);
                    drawale.setBounds(0, 0, drawale.getIntrinsicWidth(), drawale.getIntrinsicHeight());
                    return drawale;
                }
            }, null);
            toast.setText(span);
            toast.show();

使用自定义视图的Toast

Toast toast2=new Toast(getApplicationContext());
            LayoutInflater infater= getLayoutInflater();
            View toastView=infater.inflate(R.layout.my_toast, null);
            TextView textview_title=(TextView) toastView.findViewById(R.id.textView_title);
            textview_title.setText("我是标题");
            toast2.setView(toastView);
            toast2.setDuration(Toast.LENGTH_SHORT);
            toast2.show();
<!--自定义的布局-->
<?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:orientation="vertical" >
    <TextView 
        android:id="@+id/textView_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher"/>
     <TextView 
        android:id="@+id/textView_content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本内容"
        />

</LinearLayout>

显示结果

这里写图片描述

Dialog

最简单的dialog

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setIcon(R.drawable.ic_launcher).setTitle("这是标题").setMessage("这是内容").setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                }).setNeutralButton("NeutralButton",new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                }).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {


                    }
                });
                AlertDialog dialog=builder.create();
                dialog.show();

简单的dialog,显示多个数据

private String[] mData={"第一条数据","第二条数据","第三条数据","第四条数据"};

    AlertDialog.Builder builder1=new AlertDialog.Builder(MainActivity.this);
            builder1.setTitle("选择的标题");
            builder1.setItems(mData, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    Toast.makeText(getApplicationContext(), "选中了第"+(which+1), Toast.LENGTH_SHORT).show();

                }
            });
            AlertDialog dialog=builder1.create();
            dialog.show();

显示结果

这里写图片描述

单选框

private String[] mSex = { "男", "女", "其他" };
    private String sex;
    private Button button5;
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            //第一个参数是集合,第二个参数0,为默认选中第一个,赋值若超出数组的范围,则为默认不选中
            builder.setSingleChoiceItems(mSex, 0, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    sex=mSex[which];
                    Toast.makeText(getApplicationContext(), "你选中的是"+mSex[which], Toast.LENGTH_SHORT);

                }
            });
            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    button5.setText(sex);
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();

显示结果

这里写图片描述

多选框

private String[] mHobby={"游泳","跑步","篮球","足球","乒乓球 "};
    private boolean[] mCheckd=new boolean[mHobby.length];
    private StringBuffer hobby;
    private Button button6;
AlertDialog.Builder builder2=new AlertDialog.Builder(MainActivity.this);
            builder2.setMultiChoiceItems(mHobby, mCheckd, new OnMultiChoiceClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                mCheckd[which]=isChecked;

                }
            });
            builder2.setNegativeButton("取消", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // TODO Auto-generated method stub

                }
            });
            builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    hobby=new StringBuffer();
                    for(int i=0;i<mHobby.length;i++){
                        if(mCheckd[i]){
                            hobby.append(mHobby[i]);
                        }
                    }

                    button6.setText(hobby);
                }
            });
            AlertDialog dialog2 = builder2.create();
            dialog2.show();

结果图

这里写图片描述

自定义的Dialog

代码部分

"注意这次用的是Dialog,而且加了style"
//
Dialog mydialog =new Dialog(MainActivity.this,R.style.NoDialogTitle);
            LayoutInflater inflate=getLayoutInflater();
            View dialogview=inflate.inflate(R.layout.mydialog, null);
            TextView text_tile=(TextView) dialogview.findViewById(R.id.text_view_title);
            TextView text_content=(TextView) dialogview.findViewById(R.id.text_view_content);
            text_tile.setText("太坑了");
            text_content.setText("的确是啊");
            Button button_ok=(Button) dialogview.findViewById(R.id.dialog_ok);
            Button button_cancel=(Button) dialogview.findViewById(R.id.dialog_cancel);
            button_ok.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "你点击了确定", Toast.LENGTH_SHORT).show();

                }
            });
            button_cancel.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "你点击了取消", Toast.LENGTH_SHORT).show();

                }
            });
            mydialog.setContentView(dialogview);
            mydialog.show();

button的背景设置(可以自己画矩形的形状)

<!--button_normal-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
    <corners android:bottomLeftRadius="15dp"/>
    <solid android:color="#79dff6"/>
    <stroke android:color="#fff" android:width="2dp"/>
</shape>

<!--button_press-->
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
     <corners android:bottomLeftRadius="15dp"/>
    <solid android:color="#122851"/>
    <stroke android:color="#fff" android:width="2dp"/>

</shape>

<!--button_background-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/button_press" android:state_pressed="true"/>
    <item android:drawable="@drawable/button_normal"></item>

</selector>

对话框的背景

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
     <corners android:radius="15dp"/>
    <solid android:color="#79dff6"/>
    <stroke android:color="#fff" android:width="2dp"/>

</shape>

另外在Style 中添加

 <style  name="NoDialogTitle" parent="@android:Theme.Dialog">

        <item name="android:windowFrame">@null</item>

        <item name="android:windowNoTitle">true</item>

        <item name="android:windowBackground">@android:color/transparent</item>

        <item name="android:windowIsFloating">true</item>

        <item name="android:windowContentOverlay">@null</item>

    </style>

Dialog布局

<?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:orientation="vertical" 
    android:background="@drawable/dialog_background">
    <TextView 
        android:id="@+id/text_view_title"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:text="我是标题"/>
     <TextView 
        android:id="@+id/text_view_content"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:padding="10dp"
        android:text="我是内容"/>
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:id="@+id/dialog_ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_background"
            android:text="确定"/>
        <Button
            android:id="@+id/dialog_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/button_background"
            android:text="取消"/>
    </LinearLayout>
</LinearLayout>

运行图

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值