使用Toast顯示提示資訊框
阿新 • • 發佈:2019-01-02
package com.example.toasttest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button shortBut=(Button) super.findViewById(R.id.shortToast);
Button longBut=(Button) super.findViewById(R.id.longToast);
//為短時間的Toast新增點選事件
shortBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//建立一個Toast提示資訊
Toast toast=Toast.makeText(MainActivity.this, "簡單的提示資訊", Toast.LENGTH_SHORT);
toast.show();
}
});
//為圖片的toast新增監聽
longBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast toast=Toast.makeText(MainActivity.this, "帶圖片的提示資訊", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
//獲取Toast提示裡邊原有的View
View toastView=toast.getView();
//建立一個ImageView
ImageView image=new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
//建立一個LinearLayout容器
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.addView(image);
ll.addView(toastView);
ll.setOrientation(LinearLayout.HORIZONTAL);
toast.setView(ll);
toast.show();
}
});
}
}
import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button shortBut=(Button) super.findViewById(R.id.shortToast);
Button longBut=(Button) super.findViewById(R.id.longToast);
//為短時間的Toast新增點選事件
shortBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//建立一個Toast提示資訊
Toast toast=Toast.makeText(MainActivity.this, "簡單的提示資訊", Toast.LENGTH_SHORT);
toast.show();
}
});
//為圖片的toast新增監聽
longBut.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast toast=Toast.makeText(MainActivity.this, "帶圖片的提示資訊", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER,0,0);
//獲取Toast提示裡邊原有的View
View toastView=toast.getView();
//建立一個ImageView
ImageView image=new ImageView(MainActivity.this);
image.setImageResource(R.drawable.ic_launcher);
//建立一個LinearLayout容器
LinearLayout ll=new LinearLayout(MainActivity.this);
ll.addView(image);
ll.addView(toastView);
ll.setOrientation(LinearLayout.HORIZONTAL);
toast.setView(ll);
toast.show();
}
});
}
}