Android仿QQ在状态栏显示登录状态效果

 更新时间:2017年12月25日 17:03:31   作者:光仔December  
这篇文章主要介绍了Android仿QQ在状态栏显示登录状态效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

运行本实例,将显示一个用户登录界面,输入用户名(hpuacm)和密码(1111)后,单击"登录"按钮,将弹出如下图所示的选择登录状态的列表对话框,

单击代表登录状态的列表项,该对话框消失,并在屏幕的左上角显示代表登录状态的通知(如图)


过一段时间后该通知消失,同时在状态栏上显示代表该登录状态的图标(如图)


将状态栏下拉可以看到状态的详细信息(如图)


单击"更改登录状态"按钮,将显示通知列表。单击"退出"按钮,可以删除该通知。

具体实现方法:

此处是一个登陆界面
res/layout/main.xml:

<?xml version="1.0" encoding="utf-8"?> 
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent" 
 android:id="@+id/tableLayout1" 
 android:gravity="center_vertical" 
 android:background="#000000" 
 android:stretchColumns="0,3" 
 > 
 <!-- 第一行 --> 
 <TableRow android:id="@+id/tableRow1" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"> 
  <TextView/> 
  <TextView android:text="用户名" 
   android:id="@+id/textView1" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:textSize="24px" 
   android:textColor="#FFFFFF"/> 
  <EditText android:id="@+id/editView1" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:background="#FFFFFF" 
   android:minWidth="200px"/> 
  <TextView/> 
 </TableRow> 
 <!-- 第二行 --> 
 <TableRow android:id="@+id/tableRow2" 
  android:layout_marginTop="10px" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"> 
  <TextView/> 
  <TextView android:text="密 码:" 
   android:id="@+id/textView2" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:textSize="24px" 
   android:textColor="#FFFFFF"/> 
  <EditText android:id="@+id/editView2" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content" 
   android:background="#FFFFFF" 
   android:textSize="24px" 
   android:inputType="textPassword"/> 
  <TextView/> 
 </TableRow> 
 <!-- 第三行 --> 
 <TableRow android:id="@+id/tableRow3" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"> 
  <TextView/> 
  <Button android:text="登录" 
   android:id="@+id/button1" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"/> 
  <Button android:text="退出" 
   android:id="@+id/button2" 
   android:layout_width="wrap_content" 
   android:layout_height="wrap_content"/> 
  <TextView/> 
 </TableRow> 
</TableLayout> 

效果如图

编写用于布局列表项内容的XML布局文件items.xml,在该文件中,采用水平线形布局管理器,并在该布局管理器中添加ImageView组件和一个TextView组件,分别用于显示列表项中的图标和文字。
res/layout/items.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical" > 
 <ImageView 
  android:id="@+id/image" 
  android:paddingLeft="10px" 
  android:paddingTop="20px" 
  android:paddingBottom="20px" 
  android:adjustViewBounds="true" 
  android:maxWidth="72px" 
  android:maxHeight="72px" 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content"/> 
 <TextView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  android:padding="10px" 
  android:layout_gravity="center" 
  android:id="@+id/title"/> 
</LinearLayout> 

MainActivity:

package com.example.test; 
 
import java.util.ArrayList; 
import java.util.HashMap; 
import java.util.List; 
import java.util.Map; 
 
 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.AlertDialog.Builder; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.SimpleAdapter; 
import android.widget.TableRow; 
 
public class MainActivity extends Activity { 
 //第一个通知的ID 
 final int NOTIFYID_1=123; 
 //用户名 
 private String user="匿名"; 
 //定义通知管理器对象 
 private NotificationManager notificationManager; 
 @Override 
 public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
   
  //获取通知管理器,用于发送通知 
  notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE); 
  Button button1=(Button)findViewById(R.id.button1);//获取登录按钮 
  //为登录按钮添加单击事件监听 
  button1.setOnClickListener(new OnClickListener() { 
    
   @Override 
   public void onClick(View view) { 
     EditText etUser=(EditText)findViewById(R.id.editView1); 
    if(!"".equals(etUser.getText())){ 
     user=etUser.getText().toString(); 
    } 
    sendNotification();//发送通知 
   } 
  }); 
   
  //获取退出按钮 
  Button button2=(Button)findViewById(R.id.button2); 
  //为退出按钮添加单击事件监听器 
  button2.setOnClickListener(new OnClickListener() { 
    
   @Override 
   public void onClick(View view) { 
     notificationManager.cancel(NOTIFYID_1); 
    //让布局中的第一行显示 
    ((TableRow)findViewById(R.id.tableRow1)).setVisibility(View.VISIBLE); 
    //让布局中的第二行显示 
    ((TableRow)findViewById(R.id.tableRow2)).setVisibility(View.VISIBLE); 
    //改变"更改登录状态"按钮上显示的文字 
    ((Button)findViewById(R.id.button1)).setText("登录"); 
   } 
  }); 
 } 
  
 /*在sendNotification方法中,首先创建一个AlertDialog.Builder对象,并为其 
  * 指定要显示的对话框的图标、标题等,然后创建两个用于保存列表项图片id和 
  * 文字的数组,并将这些图片id和文字添加到List集合中,再创建一个SimpleAdapter 
  * 简单适配器,并将该适配器作为Builder对象的适配器用于为列表对话框添加带 
  * 图标的列表项,最后创建对话框并显示。*/ 
 //发送通知 
 private void sendNotification() { 
  Builder builder=new AlertDialog.Builder(MainActivity.this); 
  builder.setIcon(R.drawable.in);//定义对话框的图标 
  builder.setTitle("我的登录状态:");//定义对话框的标题 
  final int[] imageId=new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3, 
    R.drawable.img4};//定义并初始化保存图片id的数组 
  //定义并初始化保存列表项文字的数组 
  final String[] title=new String[]{"在线","隐身","忙碌中","离线"}; 
  //创建一个List集合 
  List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>(); 
  //通过for循环将图片id和列表项文字放到Map中,并添加到List集合中 
  for(int i=0;i<imageId.length;i++){ 
   Map<String,Object> map=new HashMap<String,Object>(); 
   map.put("image", imageId[i]); 
   map.put("title",title[i]); 
   listItems.add(map); 
  } 
  final SimpleAdapter adapter=new SimpleAdapter(MainActivity.this, 
    listItems,R.layout.item,new String[]{"title","image"},new int[]{R.id.title,R.id.image}); 
  builder.setAdapter(adapter, new DialogInterface.OnClickListener() { 
    
   @Override 
   public void onClick(DialogInterface dialog, int which) { 
    Notification notify=new Notification(); 
    notify.icon=imageId[which]; 
    notify.tickerText=title[which]; 
    notify.when=System.currentTimeMillis();//设置发送时间 
    notify.defaults=Notification.DEFAULT_SOUND;//设置默认声音 
    //设置事件信息 
    notify.setLatestEventInfo(MainActivity.this, user, title[which], null); 
    //通过通知管理器发送通知 
    notificationManager.notify(NOTIFYID_1,notify); 
    //让布局中的第一行不显示 
    ((TableRow)findViewById(R.id.tableRow1)).setVisibility(View.INVISIBLE); 
    //让布局中的第二行不显示 
    ((TableRow)findViewById(R.id.tableRow2)).setVisibility(View.INVISIBLE); 
    //改变"登录"按钮上显示的文字 
    ((Button)findViewById(R.id.button1)).setText("更改登录状态"); 
   } 
  }); 
  builder.create().show();//创建对话框并显示 
 } 
} 

 运行效果和开始描述的效果相同,实现成功!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Android Https证书过期的两种解决方案

    Android Https证书过期的两种解决方案

    应该有很多小伙伴遇到这样一个问题,在线上已发布的app里,关于https的cer证书过期,从而导致app所有网络请求失效无法使用,这篇文章主要介绍了Android Https证书过期的解决方案,需要的朋友可以参考下
    2022-12-12
  • Android关键字persistent详细分析

    Android关键字persistent详细分析

    这篇文章主要介绍了Android关键字persistent的相关资料,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下
    2021-04-04
  • Android 通用型手电筒代码

    Android 通用型手电筒代码

    说到手机手电筒功能,很多人都是直接调用闪光灯,而本文给大家介绍的是用相机功能来实现的,有需要的小伙伴可以参考下。
    2015-06-06
  • Android实现悬浮窗的简单方法实例

    Android实现悬浮窗的简单方法实例

    相信大家应该也都发现了,现在很多应用都使用到悬浮窗,例如微信在视频的时候,点击Home键,视频小窗口仍然会在屏幕上显示,下面这篇文章主要给大家介绍了关于Android实现悬浮窗的简单方法,需要的朋友可以参考下
    2021-09-09
  • 利用Android实现光影流动特效的方法详解

    利用Android实现光影流动特效的方法详解

    Flutter 的画笔类 Paint 提供了很多图形绘制的配置属性,来供我们绘制更丰富多彩的图形。本篇我们引入一个 Paint 类新的属性:maskFilter,再结合之前的 shader 和动画,制作出光影流动特效,感兴趣的可以尝试一下
    2022-07-07
  • Android自定义播放器控件VideoView

    Android自定义播放器控件VideoView

    这篇文章主要介绍了Android自定义播放器控件VideoView的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android WebView开发之WebView与Native交互

    Android WebView开发之WebView与Native交互

    随着H5的广泛使用,Android开发过程中免不了会使用网页来做展示,那么web与native之间的通信就显得尤其重要了,其实际上是JavaScript与java之间的通信。本文将为大家详细介绍二者是如何实现交互的,需要的朋友可以参考一下
    2021-12-12
  • 浅析Activity启动模式

    浅析Activity启动模式

    这篇文章主要介绍了Activity启动模式的相关资料,帮助大家更好的进行Android app开发,感兴趣的朋友可以了解下
    2020-12-12
  • Android中的全局变量与局部变量使用小结

    Android中的全局变量与局部变量使用小结

    这篇文章主要介绍了Android中的全局变量与局部变量使用小结,全局变量顾名思义就是在整个的类中或者可在多个函数中调用的变量,也称为外部变量,局部变量则是特定过程或函数中可以访问的变量,需要的朋友可以参考下
    2015-01-01
  • Kotlin创建一个好用的协程作用域

    Kotlin创建一个好用的协程作用域

    这篇文章主要介绍了Kotlin创建一个好用的协程作用域,kotlin中使用协程,是一定要跟协程作用域一起配合使用的,否则可能协程的生命周期无法被准确控制,造成内存泄漏或其他问题
    2022-07-07

最新评论