toast鐨勭敤娉昦ndroid,android toast
今天小编为大家分享Windows系统下载、Windows系统教程、windows相关应用程序的文章,希望能够帮助到大家!
1、toast是Android系统中一种消息框类型
2、Android中的Toast是一种简易的消息提示框。
3、当视图显示给用户,在应用程序中显示为浮动。和Dialog不一样的是,它永远不会获得焦点,无法被点击。用户将可能是在中间键入别的东西。Toast类的思想就是尽可能不引人注意,同时还向用户显示信息,希望他们看到。而且Toast显示的时间有限,Toast会根据用户设置的显示时间后自动消失。
Toast用于向用户显示一些帮助/提示。下面我做了5中效果,来说明Toast的强大,定义一个属于你自己的Toast。
Toast.makeText(getApplicationContext(),"默认Toast样式",
toast= Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast= Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView=(LinearLayout) toast.getView();
ImageView imageCodeProject= new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
LayoutInflater inflater= getLayoutInflater();
View layout= inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image=(ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title=(TextView) layout.findViewById(R.id.tvTitleToast);
TextView text=(TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast= new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT| Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener{
Handler handler= new Handler();
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.btnSimpleToast).setOnClickListener(this);
findViewById(R.id.btnSimpleToastWithCustomPosition).setOnClickListener(
findViewById(R.id.btnSimpleToastWithImage).setOnClickListener(this);
findViewById(R.id.btnCustomToast).setOnClickListener(this);
findViewById(R.id.btnRunToastFromOtherThread).setOnClickListener(this);
Toast.makeText(getApplicationContext(),"我来自其他线程!",
Toast.makeText(getApplicationContext(),"默认Toast样式",
case R.id.btnSimpleToastWithCustomPosition:
toast= Toast.makeText(getApplicationContext(),
"自定义位置Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
case R.id.btnSimpleToastWithImage:
toast= Toast.makeText(getApplicationContext(),
"带图片的Toast", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
LinearLayout toastView=(LinearLayout) toast.getView();
ImageView imageCodeProject= new ImageView(getApplicationContext());
imageCodeProject.setImageResource(R.drawable.icon);
toastView.addView(imageCodeProject, 0);
LayoutInflater inflater= getLayoutInflater();
View layout= inflater.inflate(R.layout.custom,
(ViewGroup) findViewById(R.id.llToast));
ImageView image=(ImageView) layout
.findViewById(R.id.tvImageToast);
image.setImageResource(R.drawable.icon);
TextView title=(TextView) layout.findViewById(R.id.tvTitleToast);
TextView text=(TextView) layout.findViewById(R.id.tvTextToast);
text.setText("完全自定义Toast");
toast= new Toast(getApplicationContext());
toast.setGravity(Gravity.RIGHT| Gravity.TOP, 12, 40);
toast.setDuration(Toast.LENGTH_LONG);
case R.id.btnRunToastFromOtherThread:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:padding="5dip" android:gravity="center">
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/btnSimpleToast"
android:text="默认"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="自定义显示位置"
android:id="@+id/btnSimpleToastWithCustomPosition"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:id="@+id/btnSimpleToastWithImage"
android:text="带图片"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="完全自定义"
android:id="@+id/btnCustomToast"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="fill_parent" android:text="其他线程"
android:id="@+id/btnRunToastFromOtherThread"></Button>
<?xml version="1.0" encoding="utf-8"?>
xmlns:android="schemas.android.com/apk/res/android"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#ffffffff" android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:background="#bb000000"
android:id="@+id/tvTitleToast"/>
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/llToastContent"
android:layout_marginLeft="1dip"
android:layout_marginRight="1dip"
android:layout_marginBottom="1dip"
android:layout_width="wrap_content"
android:background="#44000000">
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:id="@+id/tvImageToast"/>
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/tvTextToast"/>
wwW.Xtw.Com.cN系统网专业的PC、手机系统开发下载平台,HarmonyOS系统、安卓、OS、windows电脑重装系统在线下载安装,操作系统平台技术学习,攻略教程,技术交流。
免责声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。内容仅供参考使用,不准确地方联系删除处理!
联系邮箱:773537036@qq.com