zoukankan      html  css  js  c++  java
  • Android学习笔记 Toast屏幕提示组件的使用方法

    activity_main.xml

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >
        
        <Button    
            android:id="@+id/btn01"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Toast按钮1" />
        <Button    
            android:id="@+id/btn02"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Toast按钮2" />
        
        <Button    
            android:id="@+id/btn03"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="自定义组件的显示框" />
    </LinearLayout>

    MainActivity.java

    public class MainActivity extends Activity {
        private Button btn01=null;
        private Button btn02=null;
        private Button btn03=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            this.btn01=(Button)super.findViewById(R.id.btn01);
            this.btn02=(Button)super.findViewById(R.id.btn02);
            this.btn03=(Button)super.findViewById(R.id.btn03);
            this.btn01.setOnClickListener(new OnClickListenerImpl());
            this.btn02.setOnClickListener(new OnClickListenerImpl());
            this.btn03.setOnClickListener(new OnClickListenerImpl());
        }
        
        private class OnClickListenerImpl implements OnClickListener{
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                switch (v.getId()) {
                case R.id.btn01:
                    Toast.makeText(MainActivity.this, "长时间显示的Toast信息", Toast.LENGTH_LONG).show();
                    break;
                case R.id.btn02:
                    Toast.makeText(MainActivity.this, "短时间显示的Toast信息", Toast.LENGTH_SHORT).show();
                    break;
                case R.id.btn03:
                    Toast myToast=Toast.makeText(MainActivity.this, "自定义Toast组件", Toast.LENGTH_LONG);
                    myToast.setGravity(Gravity.CENTER, 60, 30);
                    LinearLayout myToastView=(LinearLayout)myToast.getView();
                    ImageView img=new ImageView(MainActivity.this);
                    img.setImageResource(R.drawable.ic_launcher);
                    myToastView.addView(img,0); //参数0,是放在最前面
                    myToast.show();
                    break;
                default:
                    break;
                }
            }
            
        }
  • 相关阅读:
    数据库基础——EXISTS和IN
    C#基础——加密
    C#基础——派生和继承
    SQL Server——报表服务
    SQL Server——SQL Server Profiler
    UML基础——UML简介和历史
    C#基础——密码加密
    C#(ASP.NET)错误: 无法获取属性“0”的值: 对象为 null 或未定义 关键字 'user' 附近有语法错误。
    SQL Server——存储过程
    链表的声明及操作
  • 原文地址:https://www.cnblogs.com/taobox/p/3356540.html
Copyright © 2011-2022 走看看