[转]android学习----基础UI编程(三)

9. Toast--Android 专属浮动小提示

下例中,实现在一个EditView中输入一段话后,点击Button,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 元素:一个EditView 和一个Button
    <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>

④ 修改mainActivity.java 文件

import android.widget.Toast;
    public class EX_Ctrl_3 extends Activity {
    // Called when the activity is first created. 
    //声明两个对象变量(按钮与编辑文字)
    private Button mButton;
    private EditText mEditText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //通过findViewById()取得对象
        mButton=(Button)findViewById(R.id.Button_Send);
        mEditText=(EditText)findViewById(R.id.EditText_Wish);
        //设置onClickListener 给Button 对象聆听onClick 事件
        mButton.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                //声明字符串变量并取得用户输入的EditText 字符串
                Editable Str;
                Str=mEditText.getText();
                //使用CharSequence类getString()方法从XML中获取String
                CharSequence string2=getString(R.string.yourWish);
                CharSequence string3=getString(R.string.hasSend);
                //使用系统标准的makeText()方式来产生Toast 信息
                Toast.makeText( EX_Ctrl_3.this,string2+Str.toString()+string3,Toast.LENGTH
_LONG).show();
                //清空EditText
                mEditText.setText("");
            }
        });
    }
    }

⑤ 结果

关键点:

1. Toast 简介

Toast 是 Android 用来显示显示信息的一种机制。它与Dialog 不同,Toast 是没有焦点的。而且 Toast的显示时间有限,过了一定的时间会自动消失。

2.Toast 的 makeText 方法

方法一

函数原型:

public static Toast makeText (Context context, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.

参数:


     context


The context to use. Usually your Application or Activity object.


resId


The resource id of the string resource to use. Can be formatted text.


duration


How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

方法二

函数原型:

public static Toast makeText (Context context, CharSequence text, int duration)

Make a standard toast that just contains a text view.

参数:


    context


The context to use. Usually your Application or Activity object.


text


The text to show. Can be formatted text.


duration


How long to display the message. Either LENGTH_SHORT or LENGTH_LONG

10.Toast--显示View的提示

下例中,我们使用 Toast 显示一个ImageView。

示例代码

① 新建工程
② 在drawable 文件夹中添加一副png 图片:argon.png

③ 修改mainActivity.java 文件

package zyf.EX_Ctrl_3_B;
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ImageView;
    import android.widget.Toast;
    public class EX_Ctrl_3_B extends Activity {

@Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //设置主屏布
            setContentView(R.layout.main);
            //创建新Toast
            Toast showImageToast=new Toast(this);
            //创建新ImageView对象
            ImageView imageView=new ImageView(this);
            //从资源中获取图片
            imageView.setImageResource(R.drawable.argon);
            //设置Toast上的View--(ImageView)
            showImageToast.setView(imageView);
            //设置Toast显示时间
            showImageToast.setDuration(Toast.LENGTH_LONG);
            //显示Toast
            showImageToast.show();
        }
    }

④ 结果

同理得Toast显示Button TextView 等其他View的方法:

1. Toast 显示 Button 的方法

核心代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置主屏布局
    setContentView(R.layout.main);
    // 创建新Toast对象
    Toast showImageToast = new Toast(this);
    //新建Button对象
    Button button = new Button(this);
    button.setText("OK");
    // 设置Toast上的View--(Button)
    showImageToast.setView(button);
    // 设置Toast显示时间
    showImageToast.setDuration(Toast.LENGTH_LONG);
    // 显示Toast 
    showImageToast.show();
}

2. Toast 显示 TextView 的方法

核心代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // 设置主屏布局
    setContentView(R.layout.main);
    // 创建新Toast对象
    Toast showImageToast = new Toast(this);
    //新建TextView对象
    TextView text=new TextView(this);
    //设置TextView内容
    text.setText("显示在Toast中的TextView");
    // 设置Toast上的View--(TextView) 
    showImageToast.setView(text);
    // 设置Toast显示时间
    showImageToast.setDuration(Toast.LENGTH_LONG);
    // 显示Toast 
    showImageToast.show();
}

关键点:

1. Toast 的public 构造方法

函数原型:

public Toast (Context context)

Construct an empty Toast object. You must call setView(View) before you can call show().

参数:

context  The context to use. Usually your Application or Activity object 。

2. Toast 的 setView 方法

函数原型:

public void setView (View view)

Set the view to show.

延伸:

同理可以根据 public View getView() 方法返回 Toast 里的 view 。

3. Toast 的 setDuration 方法

函数原型:

public void setDuration (int duration)

Set how long to show the view for.

参数:

duration   设置显示时长

LENGTH_SHORT :Android 缺省的常量 表示较短时间

LENGTH_LONG :Android 缺省的常量 表示较长时间

4. Toast 的 show 方法

函数原型:

public void show()

Show the view for the specified duration .

11.  AlertDialog.Builder提示对话框

示例代码

① 新建工程
② 修改mainActivity.java 文件
   package zyf.EX_Ctrl_3_B;
   import android.app.Activity;
   import android.app.AlertDialog;
   import android.os.Bundle;
   public class EX_Ctrl_3_B extends Activity {
   //Called when the activity is first created.

@Override
   public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       //设置主屏布局
       setContentView(R.layout.main);
       //新建一个AlertDialog.Builder对象
       AlertDialog.Builder my_ADialog =new AlertDialog.Builder(this);
       //设置标题
       my_ADialog.setTitle("Android 提示");
       //设置显示消息
       my_ADialog.setMessage("AlertDialog.Builder提示对话框消息!!");
       //显示
       my_ADialog.show();
   }

}

③ 结果

时间: 2024-07-31 10:40:09

[转]android学习----基础UI编程(三)的相关文章

[转]android学习----基础UI编程(四)

CheckBox 的使用 RadioButton 的使用 12. CheckBox 的使用 1)通过只含有一个CheckBox的实例来学习CheckBox的使用 示例代码 ① 创建新工程② 在string.xml 中添加字符串 <?xml version="1.0" encoding="utf-8"?><resources><string name="app_name">Ex_Ctrl_4</string

[转]android学习----基础UI编程(六)

相簿浏览 Gallery 文件搜索引擎FileSearch 17. Gallery 与 衍生BaseAdapter 容器 Gallery控件,即Android的图片库控件. 需要定义一个BaseAdaper的子类(eg.ImageAdapter)来操作控制图片资源,然后在主类中通过Gallery.setAdapter(new ImageAdapter(this));来使用这个控制类. 示例代码 本例中 ImageView 和 Gallery 控件相互协作 . ① 新建项目 ② 定义layout

[转]android学习----基础UI编程(七)

自动完成输入框 AutoCompleteTextView 多内容自动完成输入框 19. AutoCompleteTextView 自动完成输入框 智能输入框 AutoCompleteTextView 1. 简介 一个可编辑的文本视图显示自动完成建议当用户键入.建议列表显示在一个下拉菜单,用户可以从中选择一项,以完成输入.建议列表是从一个数据适配器获取的数据. 2. 重要方法 clearListSelection():清除选中的列表项 dismissDropDown():如果存在关闭下拉菜单 ge

[转]android学习----基础UI编程(八)

模拟/数字/线程小时钟设计 动态输入日期与时间 日期设置/时间设置对话框 21. 模拟/数字/线程小时钟设计 AnalogClock 与DigitalClock 的原理,以及线程时钟的实现 . 示例代码 ① 新建工程② 修改man.xml 布局,添加一个AnalogClock.一个DigitalClock.一个TextView<TextView  //这个TextView 用来显示线程时钟    android:id="@+id/TextView_showTime"    and

[转]android学习----基础UI编程(五)

相框设计 :ImageView 的堆叠作用 相框设计 :ImageButton 的堆叠作用 自定义下拉菜单 :Spinner 与setDropDownViewResource 动态添加╱删除的Spinner 菜单 : ArrayList 与Widget 的依赖性 14. 专业相框设计 1)ImageView 的堆叠应用 利用 ImageView 的堆叠,将上例中的实现不同相框的更换 示例代码 ① 新建工程② 准备三张png 图片 ③ 修改main.xml 布局,添加UI 元素 <?xml ver

[转]android学习----基础UI编程(二)

7. TextView 和 EditView 共舞 预达到效果:在EditText中输入同时TextView进行输出 //前提:在main.xml中添加EditText 和 TextView控件 核心代码示例: public class EX_Ctrl_1 extends Activity {    private TextView mTextView01;    private EditText mEditText01;    // Called when the activity is fi

[转]android学习----基础UI编程(一)

1 通过 DisplayMetrics 类获取屏幕宽高 示例代码: package zyp.Activity; import android.app.Activity;import android.os.Bundle;import android.widget.*;import android.util.*; public class Test extends Activity {    //Called when the activity is first created. private T

[转]android学习----消息机制

一. 角色描述 1. Looper : 一个线程可以产生一个Looper对象,由它来管理此线程里的Message Queue(消息队列). 2. Handler : 你可以构造Handler对象来与Looper沟通,以便push新消息到Message Queue里:或者接收Looper(从Message Queue取出)所送来的消息. 3. Message Queue(消息队列) : 用来存放线程放入的消息. 4. 线程 :UI thread通常就是main thread,而Android启动程

[转]android学习总结----Activity view

什么是Activity? Activity 是用户接口程序,原则上它会提供给用户一个交互式的接口功能. 它是 android 应用程序的基本功能单元. Activity 本身是没有界面的 什么是view/ viewGroups? view/ viewGroups :表示在 android 平台上的基本用户界面单元. view :为指定的屏幕矩形区域存储布局和内容.处理尺寸和布局,绘制,焦点改变,翻屏,按键 等… viewGroups:包含并管理下级系列的views 和 下级的 viewGroup