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

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

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

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

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

編寫用于布局列表項內(nèi)容的XML布局文件items.xml,在該文件中,采用水平線形布局管理器,并在該布局管理器中添加ImageView組件和一個TextView組件,分別用于顯示列表項中的圖標(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 {
//第一個通知的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);
//獲取通知管理器,用于發(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)建一個AlertDialog.Builder對象,并為其
* 指定要顯示的對話框的圖標(biāo)、標(biāo)題等,然后創(chuàng)建兩個用于保存列表項圖片id和
* 文字的數(shù)組,并將這些圖片id和文字添加到List集合中,再創(chuàng)建一個SimpleAdapter
* 簡單適配器,并將該適配器作為Builder對象的適配器用于為列表對話框添加帶
* 圖標(biāo)的列表項,最后創(chuàng)建對話框并顯示。*/
//發(fā)送通知
private void sendNotification() {
Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.in);//定義對話框的圖標(biāo)
builder.setTitle("我的登錄狀態(tài):");//定義對話框的標(biāo)題
final int[] imageId=new int[]{R.drawable.img1,R.drawable.img2,R.drawable.img3,
R.drawable.img4};//定義并初始化保存圖片id的數(shù)組
//定義并初始化保存列表項文字的數(shù)組
final String[] title=new String[]{"在線","隱身","忙碌中","離線"};
//創(chuàng)建一個List集合
List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>();
//通過for循環(huán)將圖片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();//設(shè)置發(fā)送時間
notify.defaults=Notification.DEFAULT_SOUND;//設(shè)置默認聲音
//設(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)建對話框并顯示
}
}
運行效果和開始描述的效果相同,實現(xiàn)成功!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android App啟動圖啟動界面(Splash)的簡單實現(xiàn)代碼
這篇文章主要介紹了Android App啟動圖啟動界面(Splash)的簡單實現(xiàn)代碼,本文通過實例圖文詳解相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-05-05
Android編程布局控件之AbsoluteLayout用法實例分析
這篇文章主要介紹了Android編程布局控件之AbsoluteLayout用法,結(jié)合實例形式簡單分析了Android絕對布局AbsoluteLayout的使用技巧,需要的朋友可以參考下2015-12-12
Android學(xué)習(xí)教程之日歷庫使用(15)
這篇文章主要為大家詳細介紹了Android學(xué)習(xí)教程之日歷庫使用的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android?Camera+SurfaceView自動聚焦防止變形拉伸
這篇文章主要為大家介紹了Android自定義相機Camera+SurfaceView實現(xiàn)自動聚焦防止變形拉伸詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-01-01
Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析
這篇文章主要介紹了Android10?客戶端事務(wù)管理ClientLifecycleManager源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
Android跳轉(zhuǎn)到通訊錄獲取用戶名稱和手機號碼的實現(xiàn)思路
這篇文章主要介紹了Android跳轉(zhuǎn)到通訊錄獲取用戶名稱和手機號碼的實現(xiàn)思路,當(dāng)用戶點擊跳轉(zhuǎn)到通訊錄界面 并取通訊錄姓名和手機號碼 ,實現(xiàn)代碼簡單易懂,非常不錯感興趣的朋友一起看看吧2016-10-10

