Toast 彈出組件

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   java   使用   os   

一 ,在string.xml 中添加字串

<?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">EX_Ctrl_3</string><string name="dear">親愛的聖誕老人:</string><string name="sendmyWish">送出願望</string><string name="yourWish">你的願望:</string><string name="hasSend">已送達聖誕老人信箱!</string></resources>

二,在main.xml 布局中添加UI 元素:一個TextView、一個EditView 和一個Button

<?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"><TextViewandroid:layout_height="wrap_content"android:layout_width="fill_parent"android:id="@+id/TextView_dear"android:text="@string/dear"></TextView><EditTextandroid:layout_height="wrap_content"android:id="@+id/EditText_Wish"android:layout_width="fill_parent"></EditText><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/Button_Send"android:text="@string/sendmyWish"android:layout_marginLeft="50px"></Button></LinearLayout>

三,mainActivity.java 檔案

 1 package zyf.EX_Ctrl_3; 2 import android.app.Activity; 3 import android.os.Bundle; 4 import android.text.Editable; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.EditText; 8 import android.widget.Toast; 9 public class EX_Ctrl_3 extends Activity {10 /** Called when the activity is first created. */11 /*聲明兩個物件變數(按鈕與編輯文字)*/12 private Button mButton;13 private EditText mEditText;14 @Override15 public void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.main);18 /*通過findViewById()取得對象*/19 mButton=(Button)findViewById(R.id.Button_Send);20 mEditText=(EditText)findViewById(R.id.EditText_Wish);21 /*設定onClickListener 給Button 對象聆聽onClick 事件*/22 mButton.setOnClickListener(new Button.OnClickListener(){23 @Override24 public void onClick(View v) {25 // TODO Auto-generated method stub26 /*聲明字串變數並取得使用者輸入的EditText 字串*/27 Editable Str;28 Str=mEditText.getText();29 /*使用CharSequence類getString()方法從XML中擷取String*/30 CharSequence string2=getString(R.string.yourWish);31 CharSequence string3=getString(R.string.hasSend);32 /*使用系統標準的makeText()方式來產生Toast 資訊*/33 Toast.makeText( EX_Ctrl_3.this,string2+Str.toString()+string3,Toast.LENGTH34 _LONG).show();35 /*清空EditText*/36 mEditText.setText("");37 }38 });39 }40 }

 

聯繫我們

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