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

来源:互联网 发布:人工智能计算器v3.3.0 编辑:程序博客网 时间:2024/05/01 01:54


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

程序运行界面:

     

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

[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     android:id="@+id/RelativeLayout1"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android"  
  5.     android:orientation="vertical"  
  6.     android:layout_width="fill_parent"  
  7.     android:layout_height="fill_parent">  
  8.     <TextView  
  9.         android:textAppearance="?android:attr/textAppearanceLarge"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_width="wrap_content"  
  12.         android:id="@+id/textView1"  
  13.         android:text="name:"  
  14.         android:gravity="center"  
  15.         android:layout_alignParentTop="true"  
  16.         android:layout_alignParentLeft="true"  
  17.         android:layout_alignBottom="@+id/name"></TextView>  
  18.     <EditText  
  19.         android:layout_height="wrap_content"  
  20.         android:layout_width="wrap_content"  
  21.         android:id="@+id/name"  
  22.         android:layout_alignParentTop="true"  
  23.         android:layout_alignParentRight="true"  
  24.         android:layout_toRightOf="@+id/textView2">  
  25.         <requestFocus></requestFocus>  
  26.     </EditText>  
  27.     <TextView  
  28.         android:textAppearance="?android:attr/textAppearanceLarge"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_width="wrap_content"  
  31.         android:id="@+id/textView2"  
  32.         android:text="password:"  
  33.         android:layout_below="@+id/name"  
  34.         android:layout_alignParentLeft="true"></TextView>  
  35.     <EditText  
  36.         android:layout_height="wrap_content"  
  37.         android:layout_width="wrap_content"  
  38.         android:id="@+id/password"  
  39.         android:layout_alignTop="@+id/textView2"  
  40.         android:layout_alignParentRight="true"  
  41.         android:layout_alignLeft="@+id/name"></EditText>  
  42.     <Button  
  43.         android:layout_height="wrap_content"  
  44.         android:layout_width="wrap_content"  
  45.         android:id="@+id/button2"  
  46.         android:text="Button"  
  47.         android:layout_below="@+id/password"  
  48.         android:layout_toLeftOf="@+id/button1"></Button>  
  49.     <Button  
  50.         android:layout_height="wrap_content"  
  51.         android:layout_width="wrap_content"  
  52.         android:id="@+id/button1"  
  53.         android:text="Button"  
  54.         android:layout_alignBaseline="@+id/button2"  
  55.         android:layout_alignBottom="@+id/button2"  
  56.         android:layout_alignParentRight="true"  
  57.         android:layout_marginRight="20dp"></Button>  
  58.     <ImageView  
  59.         android:layout_width="wrap_content"  
  60.         android:src="@drawable/array_down"  
  61.         android:layout_height="wrap_content"  
  62.         android:id="@+id/show"  
  63.         android:layout_alignParentTop="true"  
  64.         android:layout_alignParentRight="true"  
  65.         android:layout_alignBottom="@+id/name"></ImageView>  
  66. </RelativeLayout>  

java源文件(MainActivity.java)

[java] view plain copy
  1. import java.io.FileOutputStream;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.ObjectInputStream;  
  5. import java.io.ObjectOutputStream;  
  6. import java.util.ArrayList;  
  7.   
  8.   
  9. import android.app.Activity;  
  10. import android.os.Bundle;  
  11. import android.view.View;  
  12. import android.view.ViewGroup;  
  13. import android.view.View.OnClickListener;  
  14. import android.widget.AdapterView;  
  15. import android.widget.ArrayAdapter;  
  16. import android.widget.EditText;  
  17. import android.widget.ImageView;  
  18. import android.widget.ListView;  
  19. import android.widget.PopupWindow;  
  20. import android.widget.AdapterView.OnItemClickListener;  
  21. import android.widget.PopupWindow.OnDismissListener;  
  22.   
  23.   
  24. public class MainActivity extends Activity implements  
  25. OnClickListener, OnItemClickListener, OnDismissListener {  
  26. private ArrayList<String> mList = new ArrayList<String>();  
  27. private EditText mEditText;  
  28. private ImageView mImageView;  
  29.   
  30.   
  31. private PopupWindow mPopup;  
  32.   
  33.   
  34. private boolean mShowing;  
  35. private ArrayAdapter<String> mAdapter;  
  36. private ListView mListView;  
  37. private boolean mInitPopup;  
  38.   
  39.   
  40. /** Called when the activity is first created. */  
  41. @Override  
  42. public void onCreate(Bundle savedInstanceState) {  
  43. super.onCreate(savedInstanceState);  
  44. setContentView(R.layout.main);  
  45.   
  46.   
  47. mEditText = (EditText) findViewById(R.id.name);  
  48.   
  49.   
  50. mImageView = (ImageView) findViewById(R.id.show);  
  51. mImageView.setOnClickListener(this);  
  52.   
  53.   
  54. init();  
  55. }  
  56.   
  57.   
  58. private void init() {  
  59. ObjectInputStream in = null;  
  60. try {  
  61. InputStream is = openFileInput("account.obj");  
  62. in = new ObjectInputStream(is);  
  63. mList = (ArrayList<String>) in.readObject();  
  64. if (mList.size() > 0) {  
  65. mEditText.setText(mList.get(0));
    mEditText.setSelection(mList.get(0).length());
  66. }  
  67. catch (Exception e) {  
  68. e.printStackTrace();  
  69. finally {  
  70. if (in != null) {  
  71. try {  
  72. in.close();  
  73. catch (IOException e) {  
  74. // TODO Auto-generated catch block  
  75. e.printStackTrace();  
  76. }  
  77. }  
  78. }  
  79. }  
  80.   
  81.   
  82. @Override  
  83. protected void onDestroy() {  
  84. super.onDestroy();  
  85. String input = mEditText.getText().toString();  
  86. mList.remove(input);  
  87. mList.add(input);  
  88. if (mList.size() > 5) {  
  89. mList.remove(5);  
  90. }  
  91. ObjectOutputStream out = null;  
  92. try {  
  93. FileOutputStream os = openFileOutput("account.obj",  
  94. MODE_PRIVATE);  
  95. out = new ObjectOutputStream(os);  
  96. out.writeObject(mList);  
  97. catch (Exception e) {  
  98.   
  99.   
  100. finally {  
  101. if (out != null) {  
  102. try {  
  103. out.close();  
  104. catch (IOException e) {  
  105. // TODO Auto-generated catch block  
  106. e.printStackTrace();  
  107. }  
  108. }  
  109. }  
  110. }  
  111.   
  112.   
  113. @Override  
  114. public void onClick(View v) {  
  115. if (v.getId() == R.id.show) {  
  116. if(mList!=null && mList.size()>0 && !mInitPopup){  
  117. mInitPopup = true;  
  118. initPopup();  
  119. }  
  120. if (mPopup != null) {  
  121. if (!mShowing) {  
  122. //这是为了防止下拉框与文本框之间产生缝隙,影响界面美化(注意适配的问题)
  123. mPopup.showAsDropDown(mEditText,0,-5);  
  124. mShowing = true;  
  125. else {  
  126. //mPopup.dismiss();
    dismissPopWindow();
  127. }  
  128. }  
  129. }  
  130. }  
  131.   
  132.   
  133. private void initPopup() {  
  134. mAdapter = new ArrayAdapter<String>(this,  
  135. android.R.layout.simple_dropdown_item_1line, mList);  
  136. mListView = new ListView(this);  
  137. //mListView.setDividerHeight(1);  
    // mListView.setBackgroundResource(R.drawable.kge_feek_bg);  
    // mListView.setCacheColorHint(0x00000000);  
  138. mListView.setAdapter(mAdapter);  
  139. mListView.setOnItemClickListener(this);  
  140. int height = ViewGroup.LayoutParams.WRAP_CONTENT;  
  141. int width = mEditText.getWidth();  
  142. System.out.println(width);  
  143. mPopup = new PopupWindow(mListView, width, height, true);  
  144. mPopup.setOutsideTouchable(true);  
  145. // TODO 背景的颜色待处理、
    // mPopup.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_bg));
    mPopup.setBackgroundDrawable(new ColorDrawable(Color.BLUE));
  146. mPopup.setOnDismissListener(this);  
  147. }  
  148.   
  149.   
  150. @Override  
  151. public void onItemClick(AdapterView<?> parent, View view,  
  152. int position, long id) {  
  153. mEditText.setText(mList.get(position));  
  154. mEditText.setSelection(mList.get(position).length());
  155. //mPopup.dismiss();
    dismissPopWindow();
  156. }  
  157.   
  158.   
  159. @Override  
  160. public void onDismiss() {  
  161. mShowing = false;  
  162. }  
  163.     public void dismissPopWindow() {  
            if(mPopup != null && mPopup.isShowing()){  
            mPopup.dismiss();  
            }  
        } 
  164. // TODO Auto-generated method stub
  165. // private class SelectAdapter extends BaseAdapter{}
  166. }  
0 0