全屏浏览
缩小浏览
回到页首

android基础---->Toast的使用

简要说明  

  Toast是一种没有交点,显示时间有限,不能与用户进行交互,用于显示提示信息的显示机制,我们可以把它叫做提示框。Toast不依赖 于Activity,也就是说,没有Activity,依然可以使用Toast。
   Android的四大组件:Activity, Service, Broadcast Receiver, Contet Provider,都是继承Context的(Context,现在大家称之为上下文,之前又被翻译为句柄),包括整个Application也是继承于Context的。Toast就是依赖于应用程序Application的Context。

 

项目结构

 

代码实现

  • MainActivity.java:
package com.example.linux.customtoasttest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

/**
 * writer: 胡红翔
 * function: 自定义toast,以及toast的简单使用
 */
public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    // toast:位置的改变
    public void toast1(View view) {
        Toast toast = Toast.makeText(MainActivity.this, "toast", Toast.LENGTH_LONG);
        toast.setGravity(Gravity.BOTTOM | Gravity.LEFT, 0, 0);
        toast.show();
    }

    //toast:自定义Toast
    public void customToast(View view) {
        LayoutInflater inflater = getLayoutInflater();
        ViewGroup viewGroup = (ViewGroup) findViewById(R.id.toast_layout_root);
        View layout = inflater.inflate(R.layout.toast_layout, viewGroup);

        TextView text = (TextView) layout.findViewById(R.id.textView);
        text.setText("This is a custom toast");

        Toast toast = new Toast(MainActivity.this);
        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout); // 自定义View
        toast.show();
    }
}
  •  activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.linux.customtoasttest.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!" />

    <Button
        android:text="ToastPosition"
        android:onClick="toast1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:text="CustomToast"
        android:onClick="customToast"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>
View Code
  •  toast_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#DAAA"
    android:orientation="horizontal"
    android:padding="8dp">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="8dp"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#FFF" />
</LinearLayout>

 

实现原理

  • toast的makeText方法:得到一些信息,生成一个View
Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null);
TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message);
tv.setText(text);

result.mNextView = v;
result.mDuration = duration;

return result;
  • toast的show方法:这个比较难,表示没怎么看懂,后续再做补充。大概的是add(view)让上述view显示,开启了一个线程不断轮询(估计是duration),启动了一个通知服务。
// 窗口管理类
mWM = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); 
......
mWM.addView(mView, mParams);
// 绑定事件
trySendAccessibilityEvent(); 

 

友情链接

 

posted @ 2016-03-06 17:20  huhx  阅读(487)  评论(0编辑  收藏  举报