Android應用開發學習—Toast使用方法大全

來源:互聯網
上載者:User

標籤:android   style   blog   http   io   ar   color   os   使用   

  Toast 是一個 View 視圖,快速的為使用者顯示少量的資訊。 Toast 在應用程式上浮動顯示資訊給使用者,它永遠不會獲得焦點,不影響使用者的輸入等操作,主要用於 一些協助 / 提示。

Toast 最常見的建立方式是使用靜態方法 Toast.makeText

我使用的是 SDK 2.2

1.  預設的顯示方式

 

Java代碼  
        // 第一個參數:當前的上下文環境。可用getApplicationContext()或this        // 第二個參數:要顯示的字串。也可是R.string中字串ID        // 第三個參數:顯示的時間長短。Toast預設的有兩個LENGTH_LONG(長)和LENGTH_SHORT(短),也可以使用毫秒如2000ms        Toast toast=Toast.makeText(getApplicationContext(), "預設的Toast", Toast.LENGTH_SHORT);        //顯示toast資訊        toast.show();

2. 自訂顯示位置

 

Toast toast=Toast.makeText(getApplicationContext(), "自訂顯示位置的Toast", Toast.LENGTH_SHORT);        //第一個參數:設定toast在螢幕中顯示的位置。我現在的設定是置中靠頂        //第二個參數:相對於第一個參數設定toast位置的橫向X軸的位移量,正數向右位移,負數向左位移        //第三個參數:同的第二個參數道理一樣        //如果你設定的位移量超過了螢幕的範圍,toast將在螢幕內靠近超出的那個邊界顯示        toast.setGravity(Gravity.TOP|Gravity.CENTER, -50, 100);         //螢幕置中顯示,X軸和Y軸位移量都是0        //toast.setGravity(Gravity.CENTER, 0, 0);         toast.show();

3. 帶圖片的

 

Toast toast=Toast.makeText(getApplicationContext(), "顯示帶圖片的toast", 3000);        toast.setGravity(Gravity.CENTER, 0, 0);         //建立圖片視圖對象        ImageView imageView= new ImageView(getApplicationContext());        //設定圖片        imageView.setImageResource(R.drawable.ic_launcher);        //獲得toast的布局        LinearLayout toastView = (LinearLayout) toast.getView();        //設定此布局為橫向的        toastView.setOrientation(LinearLayout.HORIZONTAL);        //將ImageView在加入到此布局中的第一個位置        toastView.addView(imageView, 0);        toast.show();

4. 完全自訂顯示方式

 

//Inflater意思是充氣       //LayoutInflater這個類用來執行個體化XML檔案到其相應的視圖對象的布局       LayoutInflater inflater = getLayoutInflater();       //通過制定XML檔案及布局ID來填充一個視圖對象       View layout = inflater.inflate(R.layout.custom2,(ViewGroup)findViewById(R.id.llToast));              ImageView image = (ImageView) layout.findViewById(R.id.tvImageToast);       //設定布局中圖片視圖中圖片       image.setImageResource(R.drawable.ic_launcher);              TextView title = (TextView) layout.findViewById(R.id.tvTitleToast);       //設定標題       title.setText("標題列");              TextView text = (TextView) layout.findViewById(R.id.tvTextToast);       //設定內容       text.setText("完全自訂Toast");              Toast toast= new Toast(getApplicationContext());       toast.setGravity(Gravity.CENTER , 0, 0);       toast.setDuration(Toast.LENGTH_LONG);       toast.setView(layout);       toast.show();

5. 其他線程通過 Handler 的調用

 

    //調用方法1        //Thread th=new Thread(this);        //th.start();        //調用方法2        handler.post(new Runnable() {            @Override            public void run() {                showToast();            }        });
public void showToast(){        Toast toast=Toast.makeText(getApplicationContext(), "Toast在其他線程中調用顯示", Toast.LENGTH_SHORT);        toast.show();    }
Handler handler=new Handler(){        @Override        public void handleMessage(Message msg) {            int what=msg.what;            switch (what) {            case 1:                showToast();                break;            default:                break;            }                        super.handleMessage(msg);        }    };
@Override    public void run() {        handler.sendEmptyMessage(1);    }

 

轉自:http://daikainan.iteye.com/blog/1405575

Android應用開發學習—Toast使用方法大全

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.