zoukankan      html  css  js  c++  java
  • 消息提示的三种方式

    '''''''''''''''''''''''''''1.Toast

    效果图:

    (1)activity_mian.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     xmlns:tools="http://schemas.android.com/tools"
     4     android:layout_width="match_parent"
     5     android:layout_height="match_parent"
     6     android:paddingBottom="@dimen/activity_vertical_margin"
     7     android:paddingLeft="@dimen/activity_horizontal_margin"
     8     android:paddingRight="@dimen/activity_horizontal_margin"
     9     android:paddingTop="@dimen/activity_vertical_margin"
    10     tools:context="com.example.app2.MainActivity"
    11     android:orientation="vertical">
    12 
    13     <Button
    14         android:id="@+id/bt1"
    15         android:layout_width="match_parent"
    16         android:layout_height="wrap_content"
    17         android:text="Button" />
    18     <Button
    19         android:id="@+id/bt2"
    20         android:layout_width="match_parent"
    21         android:layout_height="wrap_content"
    22         android:text="Button2" />
    23 </LinearLayout>

    2.MainAcivity.java

     1 package com.example.app2;
     2 
     3 import android.support.v7.app.AppCompatActivity;
     4 import android.os.Bundle;
     5 import android.view.Gravity;
     6 import android.view.View;
     7 import android.widget.Button;
     8 import android.widget.Toast;
     9 
    10 public class MainActivity extends AppCompatActivity {
    11     private Button button1,button2;
    12     @Override
    13     protected void onCreate(Bundle savedInstanceState) {
    14         super.onCreate(savedInstanceState);
    15         setContentView(R.layout.activity_main);
    16         button1=(Button)findViewById(R.id.bt1);
    17         button2=(Button)findViewById(R.id.bt2);
    18 
    19         button1.setOnClickListener(new View.OnClickListener() {
    20             @Override
    21             public void onClick(View v) {
    22                 Toast.makeText(MainActivity.this,"默认Toast..",Toast.LENGTH_SHORT).show();
    23                 Toast.makeText(getApplicationContext(),R.string.toast1,Toast.LENGTH_SHORT).show();
    24             }
    25         });
    26 
    27         button2.setOnClickListener(new View.OnClickListener() {
    28             @Override
    29             public void onClick(View v) {
    30                 Toast toast = Toast.makeText(MainActivity.this,"自定义Toast..",Toast.LENGTH_SHORT);
    31                 //以中心位置为原点,向右移动200,向下移动200
    32                 toast.setGravity(Gravity.CENTER,200,200);
    33                 toast.show();
    34             }
    35         });
    36     }
    37 }

    (3) 使用 

       的时候需要在

    写入

    1 <resources>
    2     <string name="app_name">app2</string>
    3     <string name="toast1">默认toast111...</string>
    4 </resources>

     2.自定义图片的Toast。

    (1)效果图:

    (2)MianAcivity.java

     1  button3.setOnClickListener(new View.OnClickListener() {
     2             @Override
     3             public void onClick(View v) {
     4                 Toast toast = Toast.makeText(MainActivity.this,"自定义图片的Toast...",Toast.LENGTH_SHORT);
     5                 ImageView imageView = new ImageView(MainActivity.this);
     6                 LinearLayout linearLayout =(LinearLayout) toast.getView();
     7                 imageView.setImageResource(R.mipmap.ic_launcher);
     8                 linearLayout.addView(imageView);
     9                 toast.setGravity(Gravity.CENTER,0,0);
    10                 toast.show();
    11 
    12             }
    13         });

     3.完全自定义的Toast

    (1)效果图:

     1  button4.setOnClickListener(new View.OnClickListener() {
     2             @Override
     3             public void onClick(View v) {
     4 
     5                 LayoutInflater layoutInflater = getLayoutInflater();
     6                 LinearLayout linearLayout =(LinearLayout) layoutInflater.inflate(R.layout.define,null);
     7                 TextView textView = (TextView) linearLayout.findViewById(R.id.tv1);
     8                 textView.setText("这是安全自定义的Toast");
     9 
    10                 ImageView imageView = (ImageView)  linearLayout.findViewById(R.id.iv);
    11                 imageView.setImageResource(R.mipmap.ic_launcher);
    12 
    13                 TextView textView1 = (TextView)  linearLayout.findViewById(R.id.tv2);
    14                 textView1.setText("www.inspur");
    15 
    16                 Toast toast = new Toast(getApplicationContext());
    17                 toast.setView(linearLayout);
    18                 toast.setDuration(Toast.LENGTH_SHORT);
    19                 toast.setGravity(Gravity.CENTER,0,0);
    20                 toast.show();
    21 
    22 
    23 
    24             }
    25         });

    '''''''''''''''''''''2.AlertDialog

    [1]:

    (1):效果图

    (2)MainActivity.java

     1 package com.example.app3;
     2 
     3 import android.content.DialogInterface;
     4 import android.support.v7.app.AlertDialog;
     5 import android.support.v7.app.AppCompatActivity;
     6 import android.os.Bundle;
     7 import android.view.View;
     8 import android.widget.Button;
     9 import android.widget.Toast;
    10 
    11 public class MainActivity extends AppCompatActivity {
    12     private Button button;
    13 
    14     @Override
    15     protected void onCreate(Bundle savedInstanceState) {
    16         super.onCreate(savedInstanceState);
    17         setContentView(R.layout.activity_main);
    18         button = (Button) findViewById(R.id.bt);
    19         button.setOnClickListener(new View.OnClickListener() {
    20             @Override
    21             public void onClick(View v) {
    22                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    23                 //1.设置标题
    24                 builder.setTitle("登陆提示");
    25                 //2.调用setIcon()显示图标
    26                 builder.setIcon(R.mipmap.ic_launcher);
    27                 //3.设置对话框内容
    28                 builder.setMessage("是否登陆");
    29 
    30                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    31                     @Override
    32                     public void onClick(DialogInterface dialog, int which) {
    33                         Toast.makeText(getApplicationContext(),"您点击了确认按钮",Toast.LENGTH_SHORT).show();
    34                     }
    35                 });
    36 
    37                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    38                     @Override
    39                     public void onClick(DialogInterface dialog, int which) {
    40                         Toast.makeText(getApplicationContext(),"您点击了取消按钮",Toast.LENGTH_SHORT).show();
    41                     }
    42                 });
    43                 builder.create().show();
    44             }
    45         });
    46     }
    47 }

    [2]:

    (1)效果图: 选中之后点击确认,之后就可以显示

    (2)MianActivity.java

     1  button2.setOnClickListener(new View.OnClickListener() {
     2             @Override
     3             public void onClick(View v) {
     4                 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
     5                         .setTitle("简单列表")
     6                         .setIcon(R.mipmap.ic_launcher)
     7                         .setItems(str, new DialogInterface.OnClickListener() {
     8                             @Override
     9                             public void onClick(DialogInterface dialog, int which) {
    10                                 info=str[which];
    11                                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    12                                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    13                                     @Override
    14                                     public void onClick(DialogInterface dialog, int which) {
    15                                        textView.setText(info);
    16                                     }
    17                                 });
    18                                 builder.setNegativeButton("取消",null);
    19                                 builder.create().show();
    20                             }
    21                         })
    22 
    23                         .create();
    24                 alertDialog.show();
    25 
    26             }
    27         });

    [3]:

    (1)效果图:点击多选按钮,点击确认,实现显示多选的选项

    (2)MainActivity.java

    声明的变量:

    1   private Button button,button2,button3;
    2     private  String[]  str={"java","xml","Android"};
    3     private String info="";
    4     private TextView textView;
    5     private boolean[] isCheck ={false,false,false};
    6     private List<String> list;

    初始化:

    1   button = (Button) findViewById(R.id.bt);
    2         button2 = (Button) findViewById(R.id.bt2);
    3         button3 = (Button) findViewById(R.id.bt3);
    4         textView = (TextView) findViewById(R.id.tv);
    5         list = new ArrayList<String>();

    主要代码:

     1  button3.setOnClickListener(new View.OnClickListener() {
     2             @Override
     3             public void onClick(View v) {
     4                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     5                 //1.设置标题
     6                 builder.setTitle("多选提示");
     7                 //2.调用setIcon()显示图标
     8                 builder.setIcon(R.mipmap.ic_launcher);
     9                 //3.设置对话框的内容
    10                 builder.setMultiChoiceItems(str, isCheck, new DialogInterface.OnMultiChoiceClickListener() {
    11                     @Override
    12                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    13                         if (isChecked){
    14                             list.add(str[which]);
    15                         }else{
    16                             list.remove(str[which]);
    17                         }
    18                     }
    19                 });
    20                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    21                     @Override
    22                     public void onClick(DialogInterface dialog, int which) {
    23                        textView.setText(list.toString());
    24                     }
    25                 });
    26 
    27                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    28                     @Override
    29                     public void onClick(DialogInterface dialog, int which) {
    30                         textView.setText(list.toString());
    31                     }
    32                 });
    33                 builder.create().show();
    34             }
    35         });
    36 
    37 
    38     }



    
    

    [5]单选效果,图片效果

    ----------------------------------总的MainActivity.java-------------------------------------

      1 package com.example.app3;
      2 
      3 import android.content.DialogInterface;
      4 import android.support.v7.app.AlertDialog;
      5 import android.support.v7.app.AppCompatActivity;
      6 import android.os.Bundle;
      7 import android.view.View;
      8 import android.widget.Button;
      9 import android.widget.ImageView;
     10 import android.widget.TextView;
     11 import android.widget.Toast;
     12 
     13 import java.util.ArrayList;
     14 import java.util.List;
     15 
     16 public class MainActivity extends AppCompatActivity {
     17     private Button button,button2,button3,button4,button5;
     18     private  String[]  str={"java","xml","Android"};
     19     private String info="";
     20     private TextView textView;
     21     private boolean[] isCheck ={false,false,false};
     22     private List<String> list;
     23 
     24     @Override
     25     protected void onCreate(Bundle savedInstanceState) {
     26         super.onCreate(savedInstanceState);
     27         setContentView(R.layout.activity_main);
     28         button = (Button) findViewById(R.id.bt);
     29         button2 = (Button) findViewById(R.id.bt2);
     30         button3 = (Button) findViewById(R.id.bt3);
     31         button4= (Button) findViewById(R.id.bt4);
     32         button5 = (Button) findViewById(R.id.bt5);
     33         textView = (TextView) findViewById(R.id.tv);
     34         list = new ArrayList<String>();
     35 
     36 
     37         button.setOnClickListener(new View.OnClickListener() {
     38             @Override
     39             public void onClick(View v) {
     40                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     41                 //1.设置标题
     42                 builder.setTitle("登陆提示");
     43                 //2.调用setIcon()显示图标
     44                 builder.setIcon(R.mipmap.ic_launcher);
     45                 //3.设置对话框内容
     46                 builder.setMessage("是否登陆");
     47 
     48                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
     49                     @Override
     50                     public void onClick(DialogInterface dialog, int which) {
     51                         Toast.makeText(getApplicationContext(),"您点击了确认按钮",Toast.LENGTH_SHORT).show();
     52                     }
     53                 });
     54 
     55                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
     56                     @Override
     57                     public void onClick(DialogInterface dialog, int which) {
     58                         Toast.makeText(getApplicationContext(),"您点击了取消按钮",Toast.LENGTH_SHORT).show();
     59                     }
     60                 });
     61                 builder.create().show();
     62             }
     63         });
     64 
     65 
     66         button2.setOnClickListener(new View.OnClickListener() {
     67             @Override
     68             public void onClick(View v) {
     69                 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
     70                         .setTitle("简单列表")
     71                         .setIcon(R.mipmap.ic_launcher)
     72                         .setItems(str, new DialogInterface.OnClickListener() {
     73                             @Override
     74                             public void onClick(DialogInterface dialog, int which) {
     75                                 info=str[which];
     76                                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     77                                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
     78                                     @Override
     79                                     public void onClick(DialogInterface dialog, int which) {
     80                                        textView.setText(info);
     81                                     }
     82                                 });
     83                                 builder.setNegativeButton("取消",null);
     84                                 builder.create().show();
     85                             }
     86                         })
     87 
     88                         .create();
     89                 alertDialog.show();
     90 
     91             }
     92         });
     93 
     94 
     95         button3.setOnClickListener(new View.OnClickListener() {
     96             @Override
     97             public void onClick(View v) {
     98                 AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
     99                 //1.设置标题
    100                 builder.setTitle("多选提示");
    101                 //2.调用setIcon()显示图标
    102                 builder.setIcon(R.mipmap.ic_launcher);
    103                 //3.设置对话框的内容
    104                 builder.setMultiChoiceItems(str, isCheck, new DialogInterface.OnMultiChoiceClickListener() {
    105                     @Override
    106                     public void onClick(DialogInterface dialog, int which, boolean isChecked) {
    107                         if (isChecked){
    108                             list.add(str[which]);
    109                         }else{
    110                             list.remove(str[which]);
    111                         }
    112                     }
    113                 });
    114                 builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
    115                     @Override
    116                     public void onClick(DialogInterface dialog, int which) {
    117                        textView.setText(list.toString());
    118                     }
    119                 });
    120 
    121                 builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
    122                     @Override
    123                     public void onClick(DialogInterface dialog, int which) {
    124                         textView.setText(list.toString());
    125                     }
    126                 });
    127                 builder.create().show();
    128             }
    129         });
    130 
    131 
    132         button4.setOnClickListener(new View.OnClickListener() {
    133             @Override
    134             public void onClick(View v) {
    135                 ImageView imageView = new ImageView(MainActivity.this);
    136                 imageView.setImageResource(R.mipmap.ic_launcher);
    137                 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
    138                         .setTitle("简单列表")
    139                         .setIcon(R.mipmap.ic_launcher)
    140                         .setView(imageView)
    141                         .setPositiveButton("确认",null)
    142                         .setNegativeButton("取消",null)
    143                         .create();
    144                 alertDialog.show();
    145 
    146             }
    147         });
    148         button5.setOnClickListener(new View.OnClickListener() {
    149             @Override
    150             public void onClick(View v) {
    151 
    152                 AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
    153                         .setTitle("单选")
    154                         .setIcon(R.mipmap.ic_launcher)
    155                         .setSingleChoiceItems(str, 0, new DialogInterface.OnClickListener() {
    156                             @Override
    157                             public void onClick(DialogInterface dialog, int which) {
    158                                 info=str[which];
    159                             }
    160                         })
    161                         .setPositiveButton("确认", new DialogInterface.OnClickListener() {
    162                             @Override
    163                             public void onClick(DialogInterface dialog, int which) {
    164                                 textView.setText(info);
    165                             }
    166                         })
    167                         .setNegativeButton("取消",null)
    168                         .create();
    169                 alertDialog.show();
    170 
    171             }
    172         });
    173 
    174 
    175     }
    176 }

    布局activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.example.app3.MainActivity"
        android:orientation="vertical">
    
        <Button
            android:id="@+id/bt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
        <Button
            android:id="@+id/bt2"
            android:text="列表效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/bt3"
            android:text="多选效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/bt4"
            android:text="图片效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/bt5"
            android:text="单选效果"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>

     ........................Notification.............

    1.效果图

    2.MainActivity.xml

     1 package com.example.administrator.hello6;
     2 
     3 import android.app.Notification;
     4 import android.app.NotificationManager;
     5 import android.app.PendingIntent;
     6 import android.content.Context;
     7 import android.content.Intent;
     8 import android.os.SystemClock;
     9 import android.provider.Settings;
    10 import android.support.v7.app.AlertDialog;
    11 import android.support.v7.app.AppCompatActivity;
    12 import android.os.Bundle;
    13 import android.view.View;
    14 import android.widget.Button;
    15 
    16 public class MainActivity extends AppCompatActivity {
    17     private Button bt1,bt2;
    18     private NotificationManager notificationManager;
    19     Context context;
    20     private int NOCIFICIATIN_ID=1;
    21 
    22     @Override
    23     protected void onCreate(Bundle savedInstanceState) {
    24         super.onCreate(savedInstanceState);
    25         setContentView(R.layout.activity_main);
    26         bt1 = (Button) findViewById(R.id.bu1);
    27         bt2 = (Button) findViewById(R.id.bu2);
    28 
    29         context=this;
    30         notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    31 
    32         bt1.setOnClickListener(new View.OnClickListener() {
    33             @Override
    34             public void onClick(View v) {
    35                 Intent intent = new Intent(context,AAcitivity.class);
    36                 PendingIntent pendingIntent = PendingIntent.getActivities(context,0,new Intent[]{intent},0);
    37                Notification notification = new Notification.Builder(context)
    38                        //设置一系列属性
    39                        //设置打开通知时,通知自动消失
    40                        .setAutoCancel(true)
    41                        //设置图标
    42                        .setSmallIcon(R.mipmap.ic_launcher)
    43                        //设置标题
    44                        .setTicker("有新的消息")
    45                        //设置通知标题
    46                        .setContentTitle("放假了")
    47                        //设置通知内容
    48                        .setContentText("今天周五,明天休息")
    49                         //设置默认闪光灯,震动
    50                        .setDefaults(Notification.DEFAULT_ALL)
    51                        //设置系统当前时间
    52                        .setWhen(System.currentTimeMillis())
    53                         //判断当前通知要启动哪一个Intent对象
    54                         .setContentIntent(pendingIntent)
    55                        .build();
    56                 notificationManager.notify(NOCIFICIATIN_ID,notification);
    57 
    58 
    59             }
    60         });
    61 
    62         bt2.setOnClickListener(new View.OnClickListener() {
    63             @Override
    64             public void onClick(View v) {
    65                 notificationManager.cancel(NOCIFICIATIN_ID);
    66 
    67             }
    68         });
    69 
    70     }
    71 }

    2.Acivity.java

     1 package com.example.administrator.hello6;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 
     6 /**
     7  * Created by Administrator on 2018/5/11.
     8  */
     9 public class AAcitivity extends Activity {
    10     @Override
    11     protected void onCreate(Bundle savedInstanceState) {
    12         super.onCreate(savedInstanceState);
    13         setContentView(R.layout.a);
    14     }
    15 }

    3.a.xml

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent">
     5     <TextView
     6         android:text="太棒了"
     7         android:textSize="40px"
     8         android:layout_width="wrap_content"
     9         android:layout_height="wrap_content" />
    10 
    11 </LinearLayout>

    4.AndroidMainfest.xml:设置权限

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
     3     package="com.example.administrator.hello6">
     4     <uses-permission android:name="android.permission.FLASHLIGHT"></uses-permission>
     5     <uses-permission android:name="android.permission.VIBRATE"></uses-permission>
     6     <application
     7         android:allowBackup="true"
     8         android:icon="@mipmap/ic_launcher"
     9         android:label="@string/app_name"
    10         android:supportsRtl="true"
    11         android:theme="@style/AppTheme">
    12         <activity android:name=".MainActivity">
    13             <intent-filter>
    14                 <action android:name="android.intent.action.MAIN" />
    15 
    16                 <category android:name="android.intent.category.LAUNCHER" />
    17             </intent-filter>
    18         </activity>
    19         <activity android:name=".AAcitivity">
    20 
    21         </activity>
    22     </application>
    23 
    24 </manifest>
    
    
  • 相关阅读:
    sratookit
    转录组测序
    单菌基因组测序常见问题
    微生物测序样本准备方法总结
    Review:Microbiota, metagenome, microbiome傻傻分不清
    扩增子、宏基因组测序问题集锦
    扩增子图表解读8网络图:节点OTU或类Venn比较
    扩增子图片解读7三元图
    扩增子图表解读5火山图:差异OTU的数量及变化规律
    扩增子图表解读6韦恩图:比较组间共有和特有OTU或分类单元
  • 原文地址:https://www.cnblogs.com/sunxiaoyan/p/9013074.html
Copyright © 2011-2022 走看看