Toast使用方法——简单基础编
Toast内容显示图
package com.smart; import android.app.Activity; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; public class Main extends Activity implements OnClickListener{ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);//得到参数 Button btnTextToast = (Button) findViewById(R.id.btnTextToast); Button btnImageToast = (Button) findViewById(R.id.btnImageToast); btnTextToast.setOnClickListener(this);//绑定器 btnImageToast.setOnClickListener(this); } //点击方法事件 @Override public void onClick(View v) { switch(v.getId()){ case R.id.btnTextToast: //使用TOAST方法显示结果内容 Toast textToast=Toast.makeText(this, "新年好,我的朋友们,\n哈,哈,哈!", Toast.LENGTH_LONG); textToast.show(); break; case R.id.btnImageToast: View view = getLayoutInflater().inflate(R.layout.toast, null); TextView textView = (TextView) view.findViewById(R.id.smart); //使用TOAST方法显示结果内容 textView.setText("新年好,我的朋友们,\n哈,哈,哈!"); Toast toast = new Toast(this); toast.setDuration(Toast.LENGTH_LONG); toast.setView(view); toast.show(); break; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Button android:id="@+id/btnTextToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SMART文本的Toast" /> <Button android:id="@+id/btnImageToast" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SMART图片的Toast" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#555" android:layout_margin="10dp" > <ImageView android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/face" /> <TextView android:id="@+id/smart" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="10dp" /> </LinearLayout>