android 開發-Toast控制項的實現

來源:互聯網
上載者:User

標籤:

  • Toast多士:

  Toast內容簡單,不做過多介紹,Toast支援內建簡單多士,自訂多士。內容簡單可見代碼,詳見API。A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. For example, navigating away from an email before you send it triggers a "Draft saved" toast to let you know that you can continue editing later. Toasts automatically disappear after a timeout.

  •   程式碼範例

  activity_main.xml

    

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2     xmlns:tools="http://schemas.android.com/tools" 3     android:layout_width="match_parent" 4     android:layout_height="match_parent" 5     android:paddingBottom="@dimen/activity_vertical_margin" 6     android:paddingLeft="@dimen/activity_horizontal_margin" 7     android:paddingRight="@dimen/activity_horizontal_margin" 8     android:paddingTop="@dimen/activity_vertical_margin" 9     tools:context=".MainActivity" >10 11     <Button12         android:id="@+id/button1"13         android:layout_width="wrap_content"14         android:layout_height="wrap_content"15         android:layout_alignParentTop="true"16         android:layout_centerHorizontal="true"17         android:layout_marginTop="190dp"18         android:text="多士按鈕" />19 20     <Button21         android:id="@+id/button2"22         android:layout_width="wrap_content"23         android:layout_height="wrap_content"24         android:layout_alignLeft="@+id/button1"25         android:layout_below="@+id/button1"26         android:layout_marginTop="20dp"27         android:text="自訂多士" />28 29 </RelativeLayout>

 

  toast_layout.xml

    

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:id="@+id/toast_layout_root"              android:orientation="horizontal"              android:layout_width="fill_parent"              android:layout_height="fill_parent"              android:padding="8dp"              android:background="#DAAA">                  <ImageView android:src="@drawable/droid"               android:layout_width="wrap_content"               android:layout_height="wrap_content"               android:layout_marginRight="8dp"               />    <TextView android:id="@+id/text"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:textColor="#FFF"              /></LinearLayout>

  Activity

 1 package com.example.android_toast; 2  3 import android.app.Activity; 4 import android.os.Bundle; 5 import android.view.Gravity; 6 import android.view.Menu; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.view.ViewGroup;10 import android.widget.Button;11 import android.widget.TextView;12 import android.widget.Toast;13 /**14  * @author xiaowu15  * NOTE:Toast多士,支援內建簡單多士,自訂多士。內容簡單可見代碼,詳見API16  */17 public class MainActivity extends Activity {18     private Button button ;19     private Button button2 ;20     @Override21     protected void onCreate(Bundle savedInstanceState) {22         super.onCreate(savedInstanceState);23         setContentView(R.layout.activity_main);24         button = (Button) this.findViewById(R.id.button1);25         button2 = (Button)this.findViewById(R.id.button2);26         27         //為按鈕添加點擊監聽事件28         button.setOnClickListener(new View.OnClickListener() {29             @Override30             public void onClick(View v) {31                 // TODO Auto-generated method stub32                 //Toast.makeText(MainActivity.this, "多士內容", 1).show();                33                 //建立多士對象34                 Toast toast =Toast.makeText(MainActivity.this, "多士內容", 0);35                 //設定多士在視圖中顯示的位置36                 toast.setGravity(Gravity.BOTTOM|Gravity.CENTER, 0, 0);37                 toast.show();38             }39         });40         41         //為按鈕添加點擊監聽事件42         button2.setOnClickListener(new OnClickListener() {43             @Override44             public void onClick(View v) {45                 // TODO Auto-generated method stub46                 //通過xml設定檔,載入自訂多士視圖47                 View view = getLayoutInflater().inflate(R.layout.toast_layout, (ViewGroup) findViewById(R.id.toast_layout_root));48                 //通過該視圖查詢檢視表中的對象textView49                 TextView textView = (TextView) view.findViewById(R.id.text);50                 textView.setText("多士內容");51 //                Toast toast = new Toast(MainActivity.this);52                 Toast toast = new Toast(getApplicationContext());53                 //設定多士在視圖中顯示的位置54                 toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);55                 //設定多士限時時間長度56                 toast.setDuration(Toast.LENGTH_LONG);57                 toast.setView(view);58                 toast.show();59             }60         });61         62     }63 64     @Override65     public boolean onCreateOptionsMenu(Menu menu) {66         // Inflate the menu; this adds items to the action bar if it is present.67         getMenuInflater().inflate(R.menu.main, menu);68         return true;69     }70 71 }

 

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.