关闭

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

94人阅读 评论(0) 收藏 举报
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;
}
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1218次
    • 积分:38
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:12篇
    • 译文:0篇
    • 评论:0条