是个传颂厨

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

学习自慕课网,不过感觉老师这个章节讲的不认真。。。

首先写布局,用户名,编辑框,密码,编辑框,是否保存,登录按钮,取消按钮(没用来着)

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
     >
     <TextView 
         android:id="@+id/textview1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignParentTop="true"
         android:layout_marginTop="16dp"
         android:text="用户名"/>
     <EditText 
         android:id="@+id/eduserName"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentRight="true"
         android:layout_alignTop="@+id/textview1"
         android:layout_toRightOf="@+id/textview1"
         android:ems="10"/>

     <TextView
         android:id="@+id/textview2"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignParentLeft="true"
         android:layout_alignRight="@+id/textview1"
         android:layout_below="@+id/eduserName"
         android:layout_marginTop="16dp"
         android:text="密码" />

          <EditText
              android:id="@+id/eduserPassword"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignLeft="@+id/eduserName"
              android:layout_alignParentRight="true"
              android:layout_alignTop="@+id/textview2"
              android:ems="10" />

          <CheckBox
              android:id="@+id/chkSaveName"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/eduserPassword"
              android:text="保存用户名" />

          <Button
              android:id="@+id/btnLogin"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/chkSaveName"
              android:text="登录" />

          <Button
              android:id="@+id/btnCancel"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignBaseline="@+id/btnLogin"
              android:layout_alignBottom="@+id/btnLogin"
              android:layout_toRightOf="@+id/btnLogin"
              android:text="取消" />

</RelativeLayout>

不用xml的onclick,个人喜好而已。。。

然后

package com.example.shared;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;

public class MainActivity extends Activity {
    
    EditText eduserName,eduserPassword;
    CheckBox chk;
    SharedPreferences pref;
    Editor editor;
    Button bt1;
    Button bt2;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        eduserName = (EditText) findViewById(R.id.eduserName);
        eduserPassword = (EditText) findViewById(R.id.eduserPassword);
        chk  = (CheckBox) findViewById(R.id.chkSaveName);
        pref = getSharedPreferences("userInfo", MODE_PRIVATE);
        editor = pref.edit();//启用编辑
        String name = pref.getString("userName", "");
        if (name == null) {
            chk.setChecked(false);
        }else {
            chk.setChecked(true);
            eduserName.setText(name);
        }
        
        bt1 = (Button) findViewById(R.id.btnLogin);
        bt2 = (Button) findViewById(R.id.btnCancel);
        
        bt1.setOnClickListener(new OnClickListener() {    
            @Override
            public void onClick(View arg0) {
                String name  = eduserName.getText().toString().trim();//trim去掉首尾空格
                String pass  = eduserPassword.getText().toString().trim();
                if("admin".equals(name)&&"123456".equals(pass)){//随便设个密码先看看
                    if(chk.isChecked()){
                        editor.putString("userName", name);
                        editor.commit();//提交
                        Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_LONG).show();
                    }else{
                        editor.remove("userName");
                        editor.commit();
                    }
                }else {
                    Toast.makeText(MainActivity.this, "禁止登陆", Toast.LENGTH_LONG).show();
                }
            }
        });
        
    }
    
}

没啥实际用途只是模仿功能练习一下~

posted on 2016-09-07 23:52  是个传颂厨  阅读(276)  评论(0编辑  收藏  举报