歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android 保存用戶登錄賬號

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)

package com.ex09;


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;
}
}

Copyright © Linux教程網 All Rights Reserved