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;
                }
            }
            
        }
  • 相关阅读:
    (Windows)VMware虚拟机安装Linux系统
    C++调用HNSW实现图像配准
    (Windows) OpenCV 3.1.0 + opencv_contrib编译
    (Windows)VS2017下编译FLANN1.8.4
    C++读写txt
    数据类型转换
    tensorflow模型的保存与恢复,以及ckpt到pb的转化
    Linux系统安装MySql步骤及截屏
    实时多项式拟合
    Linux系统安装NoSQL(MongoDB和Redis)步骤及问题解决办法
  • 原文地址:https://www.cnblogs.com/taobox/p/3356540.html
Copyright © 2011-2022 走看看