标签:
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);
}
});
标签:
原文地址:http://www.cnblogs.com/cityking/p/a023.html