Android深入淺出系列之執行個體應用—彈出訊息Toast對象的使用純文字方式(一)

來源:互聯網
上載者:User

  閱讀目錄

  一:Toast

  二:實現步驟

  一:Toast

  Toast是Android專屬的提示資訊的對象,它的使用非常的簡單,但是用途卻非常的廣泛,Toast就是一個簡短的資訊,將要告訴使用者的資訊以一個浮動在最上層的View顯示,顯示之後,靜待幾秒後會自動消失,通過Toast的特性,可以在不影響使用者通話或者聽音樂的情況下,顯示給使用者資訊,對於我們開發人員來說它也是一個非常好用的Debug方式,可以在程式運行時通過Toast的方式,顯示運行變數等資訊

  我們通過在EditText控制項中填寫文字,單擊按鈕後,會發出Toast資訊。

  二:實現步驟

  1:布局檔案編寫

  1.1:布局檔案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"
      >
    <EditText 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:singleLine="true"
       android:id="@+id/et"
      />
      <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="得到資訊"
      android:id="@+id/btn1"
      />  

  </LinearLayout>

  2:代碼檔案編寫

  2. 1:MainActivity.java

  package com.menglin.toast;

  import android.app.Activity;
  import android.os.Bundle;
  import android.text.Editable;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.Toast;

  public class MainActivity extends Activity
  {
     //聲明一個Button對象
     private Button mybtn = null;
     //聲明一個EditText對象
     private EditText myedittext = null;
     @Override
     public void onCreate(Bundle savedInstanceState)
     {
        super.onCreate(savedInstanceState);

       //載入main.xml布局檔案
        setContentView(R.layout.main);
        //以findViewById()方法取得Button對象
        mybtn = (Button)findViewById(R.id.btn1);
        //以findViewById()方法取得EditText對象
        myedittext = (EditText)findViewById(R.id.et);
        //給Button對象綁定單擊監聽事件
        mybtn.setOnClickListener(listener);
     }
 
     //監聽事件
     private OnClickListener listener = new OnClickListener()
     { 
        @Override
        public void onClick(View v)
        {
           Editable str;//聲明字串變數
           //得到由使用者輸入EditText的文字內容
           str = myedittext.getText();
           //通過Toast的靜態方法makeText()建立了一個Toast對象,該方法的參數分別為上下文,顯示的文本,顯示的時間長短,顯示的時間還可以設定為Toast.LENGTH_SHORT,這樣顯示的時間會相對短一些,然後調用show()方法顯示該Toast           Toast.makeText(MainActivity.this, str.toString(), Toast.LENGTH_LONG).show();
           //清空EditText
           myedittext.setText("");
        }
     };
  }

  三:運行效果

  當我們在EditText中輸入文字,單擊按鈕後,就會彈出提示資訊

  

  

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.