android实现仿QQ登陆界面的多账号保存


android应用程序中有些使用到用户账号登录,例如QQ登录,登录界面需要用户输入账号,为了提高用户体验,应该尽量减少用户的输入操作,因此需要将用户登录过的账号保存下来,以供下次使用。保存登录账号是一个小量数据,使用Sharedpreferences或普通文件均可实现。以下程序代码保存用户使用过的5个账号。

程序运行界面:

     

布局文件(/res/layout/main.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/RelativeLayout1"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/textView1"
        android:text="name:"
        android:gravity="center"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignBottom="@+id/name"></TextView>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/name"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/textView2">
        <requestFocus></requestFocus>
    </EditText>
    <TextView
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/textView2"
        android:text="password:"
        android:layout_below="@+id/name"
        android:layout_alignParentLeft="true"></TextView>
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/password"
        android:layout_alignTop="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_alignLeft="@+id/name"></EditText>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/button2"
        android:text="Button"
        android:layout_below="@+id/password"
        android:layout_toLeftOf="@+id/button1"></Button>
    <Button
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/button1"
        android:text="Button"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="20dp"></Button>
    <ImageView
        android:layout_width="wrap_content"
        android:src="@drawable/array_down"
        android:layout_height="wrap_content"
        android:id="@+id/show"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/name"></ImageView>
</RelativeLayout>


java源文件(MainActivity.java)

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.PopupWindow.OnDismissListener;


public class MainActivity extends Activity implements
OnClickListener, OnItemClickListener, OnDismissListener {
private ArrayList<String> mList = new ArrayList<String>();
private EditText mEditText;
private ImageView mImageView;


private PopupWindow mPopup;


private boolean mShowing;
private ArrayAdapter<String> mAdapter;
private ListView mListView;
private boolean mInitPopup;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);


mEditText = (EditText) findViewById(R.id.name);


mImageView = (ImageView) findViewById(R.id.show);
mImageView.setOnClickListener(this);


init();
}


private void init() {
ObjectInputStream in = null;
try {
InputStream is = openFileInput("account.obj");
in = new ObjectInputStream(is);
mList = (ArrayList<String>) in.readObject();
if (mList.size() > 0) {
mEditText.setText(mList.get(mList.size() - 1));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


@Override
protected void onDestroy() {
super.onDestroy();
String input = mEditText.getText().toString();
mList.remove(input);
mList.add(input);
if (mList.size() > 5) {
mList.remove(0);
}
ObjectOutputStream out = null;
try {
FileOutputStream os = openFileOutput("account.obj",
MODE_PRIVATE);
out = new ObjectOutputStream(os);
out.writeObject(mList);
} catch (Exception e) {


} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


@Override
public void onClick(View v) {
if (v.getId() == R.id.show) {
if(mList!=null && mList.size()>0 && !mInitPopup){
mInitPopup = true;
initPopup();
}
if (mPopup != null) {
if (!mShowing) {
mPopup.showAsDropDown(mEditText,0,-5);
mShowing = true;
} else {
mPopup.dismiss();
}
}
}
}


private void initPopup() {
mAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, mList);
mListView = new ListView(this);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(this);
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
int width = mEditText.getWidth();
System.out.println(width);
mPopup = new PopupWindow(mListView, width, height, true);
mPopup.setOutsideTouchable(true);
mPopup.setBackgroundDrawable(getResources().getDrawable(
R.drawable.popup_bg));
mPopup.setOnDismissListener(this);
}


@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mEditText.setText(mList.get(position));
mPopup.dismiss();
}


@Override
public void onDismiss() {
mShowing = false;
}
}




 

感谢fyqjnu提供的支持和帮助 !

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值