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;
            }
        }
        
    }

 

posted @ 2013-10-08 09:16  寂静之秋  阅读(380)  评论(0编辑  收藏  举报
哈尔滨八零网