Android development learning experience (4)

In the past few days, I have looked at several implementations of message reminders in Android development, such as Toast, Notification, AlertDialog, and so on. So first add three buttons to the xml layout to implement these three functions respectively. The code is as follows:
`
public class MainActivity extends AppCompatActivity {
NotificationManager notificationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id .button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "prompt message", Toast.LENGTH_SHORT).show();

        }
    });
    notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    Button button2=(Button)findViewById(R.id.button2);
    button2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Notification notification;
            Notification.Builder builder;
            builder=new Notification.Builder(MainActivity.this);
            builder.setContentTitle("标题");
            builder.setContentText("内容");
            builder.setSmallIcon(android.R.drawable.sym_action_chat);//设置图标
            builder.setAutoCancel(true);//设置声音提醒
            builder.setDefaults(Notification.DEFAULT_ALL);//三种提醒声音、灯光、震动都打开
            notification = builder.getNotification();
            notificationManager.notify(1, notification);
        }
    });
    Button button3=(Button)findViewById(R.id.button3);
    button3.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
         final AlertDialog   alertDialog = new AlertDialog.Builder(MainActivity.this).create();
         alertDialog.setTitle("this is alertDialog");
         alertDialog.setMessage("确定退出吗?");
         alertDialog.setCancelable(true);//点击后清除
         alertDialog.setButton(DialogInterface.BUTTON_POSITIVE,"确定", new DialogInterface.OnClickListener() {
             @Override
             public void onClick(DialogInterface dialog, int which) {
             finish();//关闭程序
             }
         });
            alertDialog.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                alertDialog.dismiss();
                }
            });
            alertDialog.show();
        }
    });
}

}
`
The first button is to implement the Toast function. After clicking, a prompt message will appear, and it will disappear automatically after a period of time. Its function is relatively simple to implement. You only need to create an object through the static method makeText() in the listener, and then call the show() method to display it. The length of the prompt can be in the third of makeText(). In the parameter settings, you can choose Toast.LENGTH_SHORT and Toast.LENGTH_LONG.
The second button is to realize the Notification function, that is, to display prompt information or background running information in the notification bar. We all know that this function is very common in Android APP. The specific steps are as follows:
1. First obtain NotificationManager
2. Create Notification.Builder
3. Set Notification related parameters
4. Push notification
The third button is to implement AlertDialog. After clicking the button, a dialog box will pop up on the current interface, such as some prompt messages. The specific steps are as follows:
1. Create an AlertDialog instance through AlertDialog.Builder
2. Set the AlertDialog related parameters
3. Set the button to create a click event
4. Call the show() method to display the dialog The
above points worth noting:
1. Difference between toast and AlertDialog The toast cannot set a click event, and it will disappear automatically after a period of time. At the same time, toast can remind the user without interrupting the user's operation.
2. When setting up Notification, the getNotification() method is used. This constructor has been deprecated since API 11 (my API version has not been updated), so Notification.Builder can be used instead.
3. Using the setButton() method in AlertDialog, you can set three buttons, positive, neutral, and negative, and their positions are located on the right, middle, and left of the dialog box in turn.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324793163&siteId=291194637