Toast 弹出组件

一 ,在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"
>
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/TextView_dear"
android:text="@string/dear"></TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/EditText_Wish"
android:layout_width="fill_parent"></EditText>
<Button
android: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 @Override
15 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 @Override
24 public void onClick(View v) {
25 // TODO Auto-generated method stub
26 /*声明字符串变量并取得用户输入的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.LENGTH
34 _LONG).show();
35 /*清空EditText*/
36 mEditText.setText("");
37 }
38 });
39 }
40 }

 

转载于:https://www.cnblogs.com/xiaoli3007/p/3890644.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值