Android仿QQ在狀態(tài)欄顯示登錄狀態(tài)效果
運(yùn)行本實(shí)例,將顯示一個(gè)用戶登錄界面,輸入用戶名(hpuacm)和密碼(1111)后,單擊"登錄"按鈕,將彈出如下圖所示的選擇登錄狀態(tài)的列表對(duì)話框,

單擊代表登錄狀態(tài)的列表項(xiàng),該對(duì)話框消失,并在屏幕的左上角顯示代表登錄狀態(tài)的通知(如圖)

過一段時(shí)間后該通知消失,同時(shí)在狀態(tài)欄上顯示代表該登錄狀態(tài)的圖標(biāo)(如圖)

將狀態(tài)欄下拉可以看到狀態(tài)的詳細(xì)信息(如圖)

單擊"更改登錄狀態(tài)"按鈕,將顯示通知列表。單擊"退出"按鈕,可以刪除該通知。
具體實(shí)現(xiàn)方法:
此處是一個(gè)登陸界面
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>
效果如圖

編寫用于布局列表項(xiàng)內(nèi)容的XML布局文件items.xml,在該文件中,采用水平線形布局管理器,并在該布局管理器中添加ImageView組件和一個(gè)TextView組件,分別用于顯示列表項(xiàng)中的圖標(biāo)和文字。
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 {
//第一個(gè)通知的ID
final int NOTIFYID_1=123;
//用戶名
private String user="匿名";
//定義通知管理器對(duì)象
private NotificationManager notificationManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//獲取通知管理器,用于發(fā)送通知
notificationManager=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Button button1=(Button)findViewById(R.id.button1);//獲取登錄按鈕
//為登錄按鈕添加單擊事件監(jiān)聽
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();//發(fā)送通知
}
});
//獲取退出按鈕
Button button2=(Button)findViewById(R.id.button2);
//為退出按鈕添加單擊事件監(jiān)聽器
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);
//改變"更改登錄狀態(tài)"按鈕上顯示的文字
((Button)findViewById(R.id.button1)).setText("登錄");
}
});
}
/*在sendNotification方法中,首先創(chuàng)建一個(gè)AlertDialog.Builder對(duì)象,并為其
* 指定要顯示的對(duì)話框的圖標(biāo)、標(biāo)題等,然后創(chuàng)建兩個(gè)用于保存列表項(xiàng)圖片id和
* 文字的數(shù)組,并將這些圖片id和文字添加到List集合中,再創(chuàng)建一個(gè)SimpleAdapter
* 簡(jiǎn)單適配器,并將該適配器作為Builder對(duì)象的適配器用于為列表對(duì)話框添加帶
* 圖標(biāo)的列表項(xiàng),最后創(chuàng)建對(duì)話框并顯示。*/
//發(fā)送通知
private void sendNotification() {
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.in);//定義對(duì)話框的圖標(biāo)
builder.setTitle("我的登錄狀態(tài):");//定義對(duì)話框的標(biāo)題
final int[] imageId=new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3,
R.drawable.img4};//定義并初始化保存圖片id的數(shù)組
//定義并初始化保存列表項(xiàng)文字的數(shù)組
final String[] title=new String[]{"在線","隱身","忙碌中","離線"};
//創(chuàng)建一個(gè)List集合
List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
//通過for循環(huán)將圖片id和列表項(xiàng)文字放到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();//設(shè)置發(fā)送時(shí)間
notify.defaults=Notification.DEFAULT_SOUND;//設(shè)置默認(rèn)聲音
//設(shè)置事件信息
notify.setLatestEventInfo(MainActivity.this, user, title[which], null);
//通過通知管理器發(fā)送通知
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("更改登錄狀態(tài)");
}
});
builder.create().show();//創(chuàng)建對(duì)話框并顯示
}
}
運(yùn)行效果和開始描述的效果相同,實(shí)現(xiàn)成功!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Studio實(shí)現(xiàn)簡(jiǎn)單的QQ登錄界面的示例代碼
- Android使用友盟集成QQ、微信、微博等第三方分享與登錄方法詳解
- Android Studio實(shí)現(xiàn)第三方QQ登錄操作代碼
- Android第三方登錄之QQ登錄
- Android實(shí)現(xiàn)QQ登錄功能
- Android第三方登錄之騰訊QQ登錄的實(shí)例代碼
- Android QQ登錄界面繪制代碼
- Android實(shí)現(xiàn)QQ登錄界面遇到問題及解決方法
- Android調(diào)用第三方QQ登錄代碼分享
- Android實(shí)現(xiàn)簡(jiǎn)單QQ登錄頁面
相關(guān)文章
Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子
這篇文章介紹了Android調(diào)用系統(tǒng)的發(fā)郵件功能的小例子,有需要的朋友可以參考一下2013-08-08
android加載系統(tǒng)相冊(cè)圖片并顯示詳解
大家好,本篇文章主要講的是android加載系統(tǒng)相冊(cè)圖片并顯示詳解,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽2021-12-12
VS Code開發(fā)React-Native及Flutter 開啟無線局域網(wǎng)安卓真機(jī)調(diào)試問題
這篇文章主要介紹了VS Code開發(fā)React-Native,F(xiàn)lutter 開啟無線局域網(wǎng)安卓真機(jī)調(diào)試,需要的朋友可以參考下2020-04-04
Android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能
本文給大家分享android開發(fā)中RecyclerView模仿探探左右滑動(dòng)布局功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2017-01-01
Flutter實(shí)現(xiàn)單選,復(fù)選和開關(guān)組件的示例代碼
在App開發(fā)過程中,選擇交互是非常常見的,今天主要介紹下關(guān)于選擇的三個(gè)組件的使用:開關(guān)、單選和復(fù)選,感興趣的小伙伴可以了解一下2022-04-04
Android實(shí)現(xiàn)拍照截取和相冊(cè)圖片截取
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)拍照截取和相冊(cè)獲取圖片截取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12
Android使用Rotate3dAnimation實(shí)現(xiàn)3D旋轉(zhuǎn)動(dòng)畫效果的實(shí)例代碼
利用Android的ApiDemos的Rotate3dAnimation實(shí)現(xiàn)了個(gè)圖片3D旋轉(zhuǎn)的動(dòng)畫,圍繞Y軸進(jìn)行旋轉(zhuǎn),還可以實(shí)現(xiàn)Z軸的縮放。點(diǎn)擊開始按鈕開始旋轉(zhuǎn),點(diǎn)擊結(jié)束按鈕停止旋轉(zhuǎn)。2018-05-05
Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果
這篇文章主要介紹了Android實(shí)現(xiàn)閃屏及注冊(cè)和登錄界面之間的切換效果,實(shí)現(xiàn)思路是先分別實(shí)現(xiàn)閃屏、注冊(cè)界面、登錄界面的活動(dòng),再用Intent將相關(guān)的活動(dòng)連接起來,實(shí)現(xiàn)不同活動(dòng)之間的跳轉(zhuǎn),對(duì)android 實(shí)現(xiàn)閃屏和界面切換感興趣的朋友一起看看吧2016-11-11
Android添加用戶組及自定義App權(quán)限的方法
今天小編就為大家分享一篇Android添加用戶組及自定義App權(quán)限的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07

