码迷,mamicode.com
首页 > 其他好文 > 详细

Toast和Notification的用法

时间:2016-05-08 10:21:17      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:

Toast的用法

新建工程Toast

资源文件添加Button按钮btnShowToast

    <Button

        android:id="@+id/btnShowToast"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="显示一个较短的Toast"/>

MainActivity文件中

private Button showToastShort;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      showToastShort = (Button) findViewById(R.id.btnShowToast);

      showToastShort.setOnClickListener(new OnClickListener() {

       

        @Override

        public void onClick(View v) {

           // TODO Auto-generated method stub

           Toast.makeText(MainActivity.this, "显示一个较短的Toast", Toast.LENGTH_SHORT).show();

        }

      });

   }

 

再新建一个Button按钮btnShowToastLong

<Button

        android:id="@+id/btnShowToastLong"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="显示一个较长的Toast"/>

 

修改MainActivity文件

showToastLong = (Button) findViewById(R.id.btnShowToastLong);

      showToastLong.setOnClickListener(new OnClickListener() {

       

        @Override

        public void onClick(View v) {

           // TODO Auto-generated method stub

           Toast.makeText(MainActivity.this, "显示一个较长的Toast", Toast.LENGTH_LONG).show();

        }

      });

 

指定Toast位置

Toast toast = Toast.makeText(MainActivity.this, "显示一个较短的Toast", Toast.LENGTH_SHORT);

toast.setGravity(Gravity.CENTER, 0, 0);

toast.show();

 

给Toast指定View

添加Button按钮

<Button

        android:id="@+id/btnShowToastImage"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"

        android:text="显示一个带有图片的Toast"/>

 

 

showToastImage = (Button) findViewById(R.id.btnShowToastImage);

      showToastImage.setOnClickListener(new OnClickListener() {

       

        @Override

         public void onClick(View v) {

           // TODO Auto-generated method stub

           Toast toast = Toast.makeText(MainActivity.this, "显示一个带有图片的Toast", Toast.LENGTH_LONG);

           ImageView imageView = new ImageView(MainActivity.this);

           imageView.setImageResource(R.drawable.ic_launcher);

           toast.setView(imageView);

           toast.show();

        }

      });

 

Notification的用法

button =(Button) findViewById(R.id.button);

    button.setOnClickListener(new OnClickListener() {

     

      @Override

      public void onClick(View v) {

       // TODO Auto-generated method stub

       counter++;

       Builder builder = new NotificationCompat.Builder(MainActivity.this);

        builder.setSmallIcon(R.drawable.ic_launcher);

       builder.setContentTitle("哇哦,你有"+counter+"个新消息!");

       builder.setContentText("你已经可以创建新的Notification了");

       Notification notification = builder.build();

       NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        manager.notify(NOTIFICATION_ID,notification);

      }

    });

Toast和Notification的用法

标签:

原文地址:http://www.cnblogs.com/cityking/p/a023.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!