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;
                }
            }
            
        }
  • 相关阅读:
    第12组 Beta冲刺 (4/5)
    第12组 Beta冲刺 (3/5)
    代码用辅助多了 基础的读取config都忘记了
    wpf 动态添加控件 通过xmal实习 c#代码插入控件
    C#里调用非托管的Dll -z注意 net版本
    动态调用 类库
    c#时间的生成
    c# 第三方 修改版本号 MSBuildTasks, 解决 通配符不匹配问题
    c#.exe以管理员身份运行
    log4
  • 原文地址:https://www.cnblogs.com/taobox/p/3356540.html
Copyright © 2011-2022 走看看