Android 自定义Toast、Dialog

来源:互联网 发布:阿特兹原厂轮毂数据 编辑:程序博客网 时间:2024/06/08 15:53

开发过程中可能需要自定义一些控件,这篇先简单说一下简单的吐司、对话框的实现

一、Toast 

1.直接创建一个类MyToast,直接看代码:

public class MyToast {    private static Toast toast;    public static Toast showToast(Context context,String the_text){        toast = new Toast(context);        toast.setDuration(Toast.LENGTH_SHORT);        View view = View.inflate(context, R.layout.toast_show,null);        TextView tv_toast_text = (TextView) view.findViewById(R.id.tv_toast_text);        tv_toast_text.setText(the_text);        toast.setView(view);        toast.setGravity(Gravity.CENTER, 0, 0);        return toast;    }}

2.创建视图布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent">    <TextView        android:id="@+id/tv_toast_text"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_gravity="center"        android:background="@drawable/mytoastshape"        android:gravity="center"        android:paddingBottom="8dp"        android:paddingLeft="30dp"        android:paddingRight="30dp"        android:paddingTop="8dp"        android:textColor="#ffffff" /></LinearLayout>

3.使用的时候直接:

MyToast.showToast(ImportantActivity.this,"提示的内容").show();



二、Dialog  (只说最常用的一种,其他的同理)

1.第一种,先看图片


2.自定义代码:

public class MyDialog extends Dialog {    private Button positiveButton, negativeButton;    private TextView contenttv;    public MyDialog(Context context) {        super(context, R.style.mydialog);        View view = LayoutInflater.from(getContext()).inflate(R.layout.mydialoglayout, null);  //通过LayoutInflater获取布局        contenttv = (TextView) view.findViewById(R.id.title_this);        positiveButton = (Button) view.findViewById(R.id.acceptbtn);        negativeButton = (Button) view.findViewById(R.id.refusebtn);        setContentView(view);  //设置view    }    //设置内容    public void setContent(String content) {        contenttv.setText(content);    }    //取消按钮监听    public void setOnPositiveListener(View.OnClickListener listener){        positiveButton.setOnClickListener(listener);    }    //确定按钮监听    public void setOnNegativeListener(View.OnClickListener listener){        negativeButton.setOnClickListener(listener);    }}
3.布局:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout   xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="200dp"    android:layout_height="40dp"    android:background="@drawable/mydialogshape">    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:id="@+id/toprl"        android:layout_above="@+id/bottomoll"        >        <TextView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:id="@+id/title_this"            android:text="确定吗?"            android:textSize="17sp"            android:textColor="#333333"            android:layout_centerVertical="true"            android:layout_centerHorizontal="true" />    </RelativeLayout>    <View        android:layout_width="match_parent"        android:layout_height="1dp"        android:background="#eeeeee"        android:layout_below="@+id/toprl"        android:layout_marginBottom="10dp"        />    <RelativeLayout        android:id="@+id/bottomoll"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        android:layout_centerHorizontal="true"        android:layout_alignParentBottom="true"        >        <LinearLayout            android:layout_width="match_parent"            android:layout_height="wrap_content">            <Button                android:id="@+id/acceptbtn"                android:layout_width="wrap_content"                android:layout_height="30dp"                android:layout_marginTop="6dp"                android:layout_marginBottom="6dp"                android:layout_weight="1"                android:text="取消"                android:textColor="#999999"                android:background="@null"                android:gravity="center"                android:textSize="16sp"                />            <View                android:layout_width="1dp"                android:layout_height="match_parent"                android:background="#eeeeee"                />            <Button                android:id="@+id/refusebtn"                android:layout_width="wrap_content"                android:layout_height="30dp"                android:layout_marginTop="6dp"                android:layout_marginBottom="6dp"                android:layout_weight="1"                android:text="确定"                android:textColor="#488927"                android:background="@null"                android:textSize="16sp"                android:gravity="center"                />        </LinearLayout>    </RelativeLayout></RelativeLayout  >
3.使用:

//对话框    protected void showDialog() {        WindowManager m = getWindowManager();        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用        final MyDialog myDialog = new MyDialog(MainActivity.this);        myDialog.getWindow().setLayout((int) (d.getWidth() * 0.65), (int) (d.getHeight() * 0.2));         myDialog.setContent("是否取消上传照片?");        myDialog.show();        myDialog.setOnPositiveListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                myDialog.dismiss();            }        });        myDialog.setOnNegativeListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                myDialog.dismiss();                //点击按钮后处理逻辑                            }        });    }

原创粉丝点击