1.Toast
android中的Toast是一个非常友好方便的用户界面交互形式,通过弹出一个吐司来警告、提示用户。
Toast的系统默认形式非常简单
Toast.makeText(MainActivity.this, "This is a normal toast!",Toast.LENGTH_SHORT);
效果就是从屏幕的下方弹出一个吐司。提示 this is a normal toast!
当然我们也可以自定义toast的弹出位置甚至弹出的内容不仅只可以是文字,其他的控件也可以,自定义代码如下:
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.animation.Animation; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { LinearLayout ll = new LinearLayout(MainActivity.this); TextView tv = new TextView(MainActivity.this); tv.setText("this is a textview"); ImageView imv = new ImageView(MainActivity.this); imv.setImageResource(R.mipmap.ic_launcher); ll.addView(tv); ll.addView(imv); Toast t = Toast.makeText(MainActivity.this, "This is a normal toast!", Toast.LENGTH_SHORT); t.setGravity(Gravity.CENTER,0,0); //set the position of the toast t.setView(ll); t.show(); } }); } }
效果是弹出的toast内容包括一段文字和一张图片。
2.Notification(通知)
我们平时用的应用每天不停的给我们发通知,这个通知就是Notification。
Notification的使用方法。
import android.app.Notification; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.NotificationCompat; import android.view.Gravity; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { public static final int NOTIFIVATION_ID = 1200; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final PendingIntent pi = PendingIntent.getActivity(MainActivity.this, 0, new Intent(MainActivity.this,MainActivity.class), PendingIntent.FLAG_CANCEL_CURRENT); findViewById(R.id.button).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { NotificationCompat.Builder builer = new NotificationCompat.Builder(MainActivity.this); builer.setContentIntent(pi); //set notifiction onClick method builer.setSmallIcon(R.mipmap.ic_launcher); builer.setContentTitle("You have a new message!"); builer.setContentText("This is a message!You've made an notification!"); Notification notification = builer.build(); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); manager.notify(NOTIFIVATION_ID,notification); } }); } }
此为安卓用户界面大致知识的完结……接下来的时间学习简单的安卓系统功能!