Android Toast and Button (土司烤好了沒?)

Toast 簡單好用
可惜的是字體太小

如果不想很麻煩的自訂Toast
只好將就一下它的缺點

簡單的三個步驟
即可看到烤好的土司

1) 在xml中建立一個button
res/layout/main.xml
 <?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:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="土司烤好了沒?"  
        android:id="@+id/tButton"  
        />  
 </LinearLayout>  


2) 主程式的Button中加入Toast
src/edu.android.mytoast/MyToast.java
 package edu.android.mytoast;  
 import android.app.Activity;  
 import android.os.Bundle;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.Toast;  
 public class MyToast extends Activity {  
   /** Called when the activity is first created. */  
   @Override  
   public void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.main);  
     Button button=(Button)findViewById(R.id.tButton);  
     button.setOnClickListener(new Button.OnClickListener() {                 
                @Override  
                public void onClick(View v) {  
                     Toast t=Toast.makeText(v.getContext(), "土司烤好了", Toast.LENGTH_SHORT);  
                     t.show();  
                }  
           });  
   }  
 }  

3) 觀看結果



沒有留言:

張貼留言