toast和dialog

小属性和要注意的地方:
margin:外部离开
padding:内部离开
调试

句柄
context
application和四大组件

toast的作用范围一般是application
dialog的作用范围一般是activity
两个组件的显示都需要调用show,dialog还需要creat

toast

自定义的话用setView方法
一些练习

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ToastActivity">

    <Button
        android:id="@+id/button_toast1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示toast1,自定义富文本" />

    <Button
        android:id="@+id/button_toast2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="显示toast2,自定义view" />


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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"
        />
    <TextView
        android:id="@+id/button_toast2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="toas你好"
        />

</LinearLayout>
package com.lingzhuo.mytoast;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class ToastActivity extends Activity implements View.OnClickListener{
    private Button mButtonToast1;
    private Button mButtonToast2;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_toast);
        mButtonToast1 = (Button) findViewById(R.id.button_toast1);
        mButtonToast2 = (Button) findViewById(R.id.button_toast2);
        mButtonToast1.setOnClickListener(this);
        mButtonToast2.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button_toast1:
                Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_SHORT);
                Spanned spanned = Html.fromHtml("我是<font color='#ff0000'>一个</font>富文本<img src=''></img>", new Html.ImageGetter() {
                    @Override
                    public Drawable getDrawable(String source) {
                        Drawable drawable = getResources().getDrawable(R.mipmap.ic_launcher);
                        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
                        return drawable;
                    }
                },null);
                toast.setText(spanned);
                toast.show();
                break;
            case R.id.button_toast2:
                Toast toast2 = new Toast(getApplicationContext());
                LayoutInflater inflater = getLayoutInflater();
                View toastView = inflater.inflate(R.layout.toast_view,null);
                toast2.setView(toastView);
                toast2.setDuration(Toast.LENGTH_SHORT);
                toast2.show();
                break;
            default:
                break;

        }

    }
}

dialog

啧啧啧,自定义的明天补上

package com.lingzhuo.mydialog;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class DialogActivity extends AppCompatActivity implements View.OnClickListener{
    private Button mBtnDialog1;
    private Button mBtnDialog2;
    private Button mBtnDialog3;
    private Button mBtnDialog4;
    private String[] mData={"一","二","三","四"};
    private String[]  sexs = {"男","女","其他"};
    private String[]  hobbies = {"足球","篮球","跑步"};
    private boolean[] isHobbys = new boolean[hobbies.length];
    private String sex;
    private StringBuffer buffer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog);
        mBtnDialog1 = (Button) findViewById(R.id.button_dialog1);
        mBtnDialog2 = (Button) findViewById(R.id.button_dialog2);
        mBtnDialog3 = (Button) findViewById(R.id.button_dialog3);
        mBtnDialog4= (Button) findViewById(R.id.button_dialog4);
        mBtnDialog1.setOnClickListener(this);
        mBtnDialog2.setOnClickListener(this);
        mBtnDialog3.setOnClickListener(this);
        mBtnDialog4.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.button_dialog1:
                showDialog();
                break;
            case R.id.button_dialog2:
                showSelectDialog();
                break;
            case R.id.button_dialog3:
                singleChoice();
                break;
            case R.id.button_dialog4:
                AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
                builder.setMultiChoiceItems(hobbies, isHobbys, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        isHobbys[which] = isChecked;
                    }
                });
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        buffer = new StringBuffer();
                        for (int i = 0; i < hobbies.length; i++) {
                            if (isHobbys[i]) {
                                buffer.append(hobbies[i]);
                            }
                        }
                        mBtnDialog4.setText("你喜欢的运动是" + buffer);
                    }
                });
                Dialog dialog = builder.create();
                dialog.show();
                break;
            default:
                break;
        }
    }

    private void singleChoice() {
        AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
        builder.setSingleChoiceItems(sexs,1, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                sex = sexs[which];
                Toast.makeText(getApplicationContext(), sex, Toast.LENGTH_SHORT).show();
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mBtnDialog3.setText("你的性别是" + sex);
            }
        });
        builder.create();
        builder.show();
    }

    private void showSelectDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);
        builder.setTitle("请选择item");
        builder.setItems(mData, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "是第" + which + "个条目", Toast.LENGTH_SHORT).show();
            }
        });
        builder.create();
        builder.show();
    }

    private void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(DialogActivity.this);   //dialog的作用范围是activity
        builder.setIcon(R.mipmap.ic_launcher).setTitle("dialog").setMessage("我是内容").setNegativeButton("NegativeButton", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(), "NegativeButton", Toast.LENGTH_SHORT).show();  //toast的作用范围一般是application
            }
        }).setPositiveButton("PositiveButton", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"PositiveButton",Toast.LENGTH_SHORT).show();
            }
        });
        builder.create();                   //必须使用creat方法
        builder.show();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值