android的toast
① Android開發,彈出提示框「Toast」是什麼問題
Android開發,彈出提示框「Toast」是因為輸入了下面這句操作命令:
Toast.makeText(getApplicationContext(),"你的提示內容",Toast.LENGTH_SHORT).show();
Android開發操作如下:
先導入:
import android.widget.Toast;
關鍵代碼:
Toast.makeText(getApplicationContext(),"提示內容",Toast.LENGTH_SHORT).show();
例子:
在一個activity中,只有一個button,單擊這個button彈出「單擊完成」提示框。
提示:
只需在onCreante方法中添加button的單擊事件
完整代碼:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toast1);
//設置button的單擊事件
findViewById(R.id.btnToast).setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//彈出提示框
Toast.makeText(getApplicationContext(),"單擊完成",Toast.LENGTH_SHORT).show();
}
});
}
② 如何在Android開發中熟練使用五種Toast的特效
Toast是Android中用來顯示顯示信息的一種機制,和Dialog不一樣的是,Toast是沒有焦點的,而且Toast顯示的時間有限,過一定的時間就會自動消失。默認效果,代碼為:
Toast.makeText(getApplicationContext(), "默認Toast樣式", Toast.LENGTH_SHORT).show();
自定義顯示位置效果,代碼為:
toast = Toast.makeText(getApplicationContext(), "自定義位置Toast", Toast.LENGTH_LONG); toast.setGravity(Gravity.CENTER, 0, 0); toast.show();
帶圖片效果,代碼為:
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); toast.show();
完全自定義效果,代碼為:
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); title.setText("Attention"); 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); toast.setView(layout); toast.show();
其他線程,代碼為:
new Thread(new Runnable() { public void run() { showToast(); } }).start();