标签:
================
几乎所有应用都会涉及到登录界面,那么就会有相应的记住密码和自动登录选项。我主要采用了SharedPreference来实现,代码比较简单。如果有什么不对的地方也希望指正,第一次发帖哈O(∩_∩)O!<ignore_js_op> Longin.rar
下面是main.xml的代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/chat_bg_default"
>
<TextView
android:layout_height="50dp"
android:layout_width="fill_parent"
android:gravity="center"
android:textSize="24sp"
android:text="登录界面"
android:textColor="@color/white"
android:background="@drawable/song_index_item_bg"/>
<ImageView
android:id="@+id/iv"
android:layout_height="220dp"
android:layout_width="230dp"
android:layout_marginTop="110dp"
android:src="@drawable/background"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<TextView
android:id="@+id/tv_name"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:layout_marginTop="7dp"
android:text="账号:"
android:layout_alignLeft="@+id/iv"
android:layout_alignTop="@+id/iv"
android:layout_marginLeft="7dp"
android:textColor="@color/green"/>
<EditText
android:id="@+id/et_name"
android:layout_height="25dp"
android:layout_width="210dp"
android:layout_marginTop="7dp"
android:layout_below="@+id/tv_name"
android:background="@drawable/shape"
android:layout_alignLeft="@+id/tv_name"/>
<TextView
android:id="@+id/tv_pass"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="15sp"
android:text="密码:"
android:layout_alignLeft="@+id/tv_name"
android:layout_below="@+id/et_name"
android:layout_marginTop="7dp"
android:textColor="@color/green"/>
<EditText
android:id="@+id/et_pass"
android:layout_height="25dp"
android:layout_width="210dp"
android:background="@drawable/shape"
android:layout_marginTop="7dp"
android:layout_below="@+id/tv_pass"
android:layout_alignLeft="@+id/tv_name"/>
<CheckBox
android:id="@+id/isremenber"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignLeft="@+id/tv_name"
android:layout_marginTop="10dp"
android:text="记住密码"
android:textSize="11sp"
android:textColor="@color/huise"
android:layout_below="@+id/et_pass"
android:button="@drawable/mycheckbox"/>
<CheckBox
android:id="@+id/isloginself"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignRight="@+id/et_name"
android:layout_marginTop="10dp"
android:text="自动登录"
android:textSize="11sp"
android:textColor="@color/huise"
android:layout_below="@+id/et_pass"
android:button="@drawable/mycheckbox"/>
<Button
android:id="@+id/longin"
android:layout_height="25dp"
android:layout_width="210dp"
android:layout_below="@+id/isloginself"
android:layout_alignLeft="@+id/et_pass"
android:layout_marginTop="10dp"
android:text="登录"
android:gravity="center"
android:background="@drawable/loginbtn"/>
</RelativeLayout>
主activity也比较简单 直接上代码:
public class LonginActivity extends Activity {
private EditText name;
private EditText pass;
private CheckBox isRemenber;
private CheckBox isLoginSelf;
private Button longin;
private ProgressDialog mDialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
name=(EditText)findViewById(R.id.et_name);
pass=(EditText)findViewById(R.id.et_pass);
isRemenber=(CheckBox)findViewById(R.id.isremenber);
isLoginSelf=(CheckBox)findViewById(R.id.isloginself);
longin=(Button)findViewById(R.id.longin);
final SharedPreferences pre=getSharedPreferences("longinvalue", MODE_WORLD_WRITEABLE);
if(pre!=null){
//记住了密码
if(pre.getBoolean("isrmb", false)==true){
name.setText(pre.getString("name", null));
pass.setText(pre.getString("pass", null));
isRemenber.setChecked(true) ;
}
if(pre.getBoolean("islgs", false)==true){
isLoginSelf.setChecked(true);
creatDialog();
new Thread(){
public void run() {
try {
Thread.sleep(3000);
if(mDialog.isShowing()){
mDialog.dismiss();
}
Intent intent2=new Intent(LonginActivity.this,Show.class);
startActivity(intent2);
} catch (Exception e) {
// TODO: handle exception
}
}
}.start();
}
}
isRemenber.setOnCheckedChangeListener( new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isRemenber.isChecked()==false){
isLoginSelf.setChecked(false);
}
}
});
isLoginSelf.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
isRemenber.setChecked(true);
}
});
longin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(!name.getText().toString().equals("")&&!pass.getText().toString().equals("")){
if(isLoginSelf.isChecked()){
pre.edit().putBoolean("isrmb", true).putBoolean("islgs", true).putString("name", name.getText().toString())
.putString("pass", pass.getText().toString()).commit();
}else{
if(isRemenber.isChecked()){
pre.edit().putBoolean("isrmb", true).putBoolean("islgs", false).putString("name", name.getText().toString())
.putString("pass", pass.getText().toString()).commit();
}else{
pre.edit().putBoolean("isrmb", false).putBoolean("islgs", false).putString("name", name.getText().toString())
.putString("pass", pass.getText().toString()).commit();
}
}
Intent intent=new Intent(LonginActivity.this,Show.class);
startActivity(intent);
}else{
Toast.makeText(getApplicationContext(), "密码或账号不能为空!", Toast.LENGTH_LONG).show();
}
}
});
}
private void creatDialog() {
// TODO Auto-generated method stub
mDialog=new ProgressDialog(this);
mDialog.setTitle("验证中");
mDialog.setMessage("正在登陆请稍后");
mDialog.setIndeterminate(true);
mDialog.setCancelable(true);
mDialog.show();
}
}<ignore_js_op>
点击登录之后就进入第二个activity ,最后使用SharedPreference 需要设置一下权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" ></uses-permission>就这么多啦,希望大家都能设计一个漂亮的登录界面!
标签:
原文地址:http://www.cnblogs.com/awkflf11/p/4346336.html