4.3给圣诞老人的信息—Toast对象的使用

本文介绍了一个简单的Toast消息显示示例,通过创建Toast对象并调用show()方法来展示用户输入的文字,实现向模拟的圣诞老人发送愿望的功能。

4.3给圣诞老人的信息—Toast对象的使用

目录

4.3给圣诞老人的信息—Toast对象的使用... 1

目标... 1

方法... 1

代码... 1

效果... 2

更多详细用法... 2

 

目标:使用Toast弹出消息.

方法:构造一个Toast对象,调用对象的show()方法

代码:

package edu.cquptzx.UseToast;

 

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;

 

publicclass UseToastActivity extends Activity {

    private Button btn;

    private EditText et;

    /** Called when the activity is first created. */

    publicvoid onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        // Find the objects by IDs

        btn = (Button) findViewById(R.id.button);

        et = (EditText) findViewById(R.id.editText);

      

        // add an listener to tha SEND BUTTON.

        btn.setOnClickListener(new OnClickListener()

        {

           publicvoid onClick(View v)

           {

              //Get the text which the user have input .

              /* 方案二:使用Editable

               * 特点,文字获取后可以很方便的进行截取,插入,等操作. */

              //Editable str;

              //str = et.getText();

             

              /* 方案一:使用String

               * 特点,文字固定,编辑不方便. */

              String str = null;

              str = et.getText().toString();

             

              Toast.makeText(getBaseContext(),

                     "Your wish: \n  \" " + str.toString() + " \" \n has been send to  Santa Claus.",

                     Toast.LENGTH_LONG)

                     .show();

           }      

        });

    }

}

效果:

UseToast

更多详细用法:

http://www.cnblogs.com/xilifeng/archive/2012/08/12/2634178.html

 

 

For more questions , contacts me by :

cquptzx@qq.com or  cquptzx@outlook.com

 

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值