Android开发的QQ登录页面

2016-04-10  本文已影响516人  Courage_SC
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sc.qq_login.MainActivity"
    tools:ignore="MergeRootFrame" 
    android:orientation="vertical"
    
    >
    <ImageView 
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/qq"
        />
    <EditText 
        android:id="@+id/qqNumber" 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入qq号"  
        />
    <EditText 
        android:id="@+id/qqPassword"
        android:inputType="textPassword"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入qq密码"
        />

    <CheckBox
        android:id="@+id/checkBoxBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="记住密码" 
        />
    <Button 
        android:id="@+id/loginBtn"
        android:text="登录"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
        
</LinearLayout>
package com.sc.qq_login;

import android.R.id;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.EditText;
import android.widget.Toast;
import android.os.Build;

public class MainActivity extends Activity implements View.OnClickListener{
    private EditText number;
    private EditText password;
    private Button login_btn;
    private CheckBox checkbox_btn;
    SharedPreferences sharedPreferences;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //初始化常见的控件
        number = (EditText)findViewById(R.id.qqNumber);
        password = (EditText)findViewById(R.id.qqPassword);
        checkbox_btn = (CheckBox)findViewById(R.id.checkBoxBtn);
        login_btn = (Button)findViewById(R.id.loginBtn);
        login_btn.setOnClickListener(this);
        
        //数据的保存使用sharedPreference,Android中提供了一个特有的数据保存机制
        //userinfo文件将会生成在应用文件夹下-------一个xml格式的文件
        //一般情况下,应用自己的数据只有当前应用可以去读,所以通常会写
        sharedPreferences = getSharedPreferences("userinfo", 0);
        //读取数据
        String usernameV = sharedPreferences.getString("username", "");
        String passwordV = sharedPreferences.getString("password", "");
        if(usernameV.length()>0&&passwordV.length()>0){
            number.setText(usernameV);
            password.setText(passwordV);
            checkbox_btn.setChecked(true);
        }
        
        
    }
    @Override
    public void onClick(View arg0) {
        int login_id = arg0.getId();
        switch (login_id) {
        case R.id.loginBtn:
        {
            String numberValue = number.getText().toString().trim();
            String passwpordValue = password.getText().toString().trim();
            if(TextUtils.isEmpty(numberValue) && TextUtils.isEmpty(passwpordValue)){
                Toast.makeText(this, "用户名和密码不能为空", 0).show();
                return;
            }else{
                Toast.makeText(this, "登录", 0).show();
            }
            //勾选checkBox的时候,将数据保存
            boolean checked = checkbox_btn.isChecked();
            if (checked) {
                Toast.makeText(this, "记住密码登录", 0).show();
                
                //向sharedPreference写数据
                Editor edit = sharedPreferences.edit();
                edit.putString("username", numberValue);
                edit.putString("password", passwpordValue);
                edit.commit();
            }else{
                Toast.makeText(this, "没记住密码登录", 0).show();
            }
            
        }
            break;

        default:
            break;
        }
        
    }
}
上一篇下一篇

猜你喜欢

热点阅读