第一天写个自定义Toast

先写个布局文件

activity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical"
 6     android:gravity="center_horizontal"
 7     >
 8 
 9     <Button 
10         android:id="@+id/btn1"
11         android:layout_width="wrap_content"
12         android:layout_height="wrap_content"
13         android:text="版权声明" />
14     <Button 
15         android:id="@+id/btn2"
16         android:layout_width="wrap_content"
17         android:layout_height="wrap_content"
18         android:text="这是我自定义的toast" />
19     
20 
21 </LinearLayout>

toast.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="wrap_content"
 5     android:orientation="horizontal" >
 6     
 7    <ImageView 
 8        android:id="@+id/img"
 9        android:layout_width="wrap_content"
10        android:layout_height="wrap_content"
11        android:src="@drawable/ic_launcher"
12        />
13 
14    <TextView 
15        android:id="@+id/tv"
16        android:layout_width="match_parent"
17        android:layout_height="wrap_content"
18        android:textSize="30sp"
19        android:text="自定义的Toast"
20        />
21 </LinearLayout>
MainActivity中的代码

package com.example.a727test;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;


import android.text.NoCopySpan.Concrete;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


1
public class MainActivity extends Activity { 2 3 private Button btn1; 4 private Button btn2; 5 6 @Override 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 btn1 = (Button) findViewById(R.id.btn1); 11 btn2 = (Button) findViewById(R.id.btn2); 12 接受和拒绝(); 13 自定义Toast(); 14 } 15 16 private void 自定义Toast() { 17 btn2.setOnClickListener(new View.OnClickListener() { 18 @Override 19 public void onClick(View arg0) { 20 Context ct = MainActivity.this;// 上下文 21 Toast toast = new Toast(ct);// 创建Toast对象 22 LayoutInflater jj = getLayoutInflater();// 渲染器 23 View zz = jj.inflate(R.layout.toast, null);// 渲染成一个View视图 24 TextView gg = (TextView) zz.findViewById(R.id.tv);// 用来显示 25 toast.setView(zz);// 26 toast.setDuration(10);// 显示时间 27 toast.show();// 展示 28 } 29 }); 30 } 31 32 private void 接受和拒绝() { 33 btn1.setOnClickListener(new View.OnClickListener() { 34 35 @Override 36 public void onClick(View arg0) { 37 AlertDialog.Builder builder = new Builder(MainActivity.this); 38 39 builder.setTitle("版权声明"); 40 builder.setIcon(R.drawable.ic_launcher); 41 builder.setMessage("该原生对话框有Google开发,版权所有,违者必究"); 42 builder.setNegativeButton("拒绝", new OnClickListener() { 43 @Override 44 public void onClick(DialogInterface arg0, int arg1) { 45 // TODO Auto-generated method stub 46 Toast.makeText(getApplicationContext(), "我拒绝", 47 Toast.LENGTH_SHORT).show(); 48 49 } 50 }); 51 builder.setPositiveButton("接受", new OnClickListener() { 52 53 @Override 54 public void onClick(DialogInterface arg0, int arg1) { 55 Toast.makeText(getApplicationContext(), "我已阅读,同意", 56 Toast.LENGTH_SHORT).show(); 57 } 58 }); 59 builder.setCancelable(false); 60 AlertDialog arg01 = builder.show(); 61 } 62 }); 63 } 64 }

 

运行结果为

1.

2.

3.

posted @ 2016-08-05 23:52  灰太狼+  阅读(142)  评论(0)    收藏  举报