<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                簡介:Toast英文含義是吐司,在Android中,它就像烘烤機里做好的吐司彈出來,并持續一小段時間后慢慢消失。 Toast也是一個容器,可以包含各種View,并承載著它們顯示。 使用場景: 1、需要提示用戶,但又不需要用戶點擊“確定”或者“取消”按鈕。 2、不影響現有Activity運行的簡單提示。 用法: 1、可以通過構造函數初始化: ~~~ //初始化Toast Toast toast = new Toast(this); //設置顯示時間,可以選擇Toast.LENGTH_LONG或者Toast.LENGTH_SHORT toast.setDuration(Toast.LENGTH_LONG); //承載一個TextView,用來顯示文字 TextView view = new TextView(this); //設置TextView的值 view.setText("這是一個Toast提示"); //設置TextView的布局 view.setLayoutParams(new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)); //Toast承載該 TextViewtoast.setView(view); //顯示Toast toast.show(); ~~~ 2、上述的方法過于復雜,一般只適用于構造特殊界面的Toast,如果只想單純的進行文字提示,可以用工廠方法,它會自動構建一個帶邊框和文字的Toast: ~~~ //利用工廠方法構造一個簡單的Toast,并鏈式結構的直接進行提示 Toast.makeText(this, "這是一個Toast提示", Toast.LENGTH_LONG).show(); ~~~ 總結:Toast可以說是最常用也是最簡單的Android控件之一,其自動關閉的功能大大簡化了代碼量,不失為用戶提示的最佳選擇。 **1. 默認的顯示方式** ![](https://box.kancloud.cn/2016-02-18_56c5a94fc0c64.jpg) ~~~ // 第一個參數:當前的上下文環境。可用getApplicationContext()或this // 第二個參數:要顯示的字符串。也可是R.string中字符串ID // 第三個參數:顯示的時間長短。Toast默認的有兩個LENGTH_LONG(長)和LENGTH_SHORT(短),也可以使用毫秒如2000ms Toast toast=Toast.makeText(getApplicationContext(), "默認的Toast", Toast.LENGTH_SHORT); //顯示toast信息 toast.show(); ~~~ **2. 自定義顯示位置** ![](https://box.kancloud.cn/2016-02-18_56c5a94fd5ac4.jpg) ~~~ 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. 帶圖片的** ![](https://box.kancloud.cn/2016-02-18_56c5a94fe6a4e.jpg) ~~~ 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. 完全自定義顯示方式** ![](https://box.kancloud.cn/2016-02-18_56c5a95007f8a.jpg) ~~~ //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 的調用** ![](https://box.kancloud.cn/2016-02-18_56c5a9501c5ff.jpg) ~~~ //調用方法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://www.cnblogs.com/wt616/archive/2011/06/20/Android_Toast.html](http://www.cnblogs.com/wt616/archive/2011/06/20/Android_Toast.html "http://www.cnblogs.com/wt616/archive/2011/06/20/Android_Toast.html") [http://daikainan.iteye.com/blog/1405575](http://daikainan.iteye.com/blog/1405575 "http://daikainan.iteye.com/blog/1405575")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看