简单登陆界面

本文详细介绍了登录界面的布局设计,包括使用XML文件定义界面组件,如按钮、文本框、复选框等,并通过Java代码实现账号、密码输入验证及登录逻辑。同时,实现了登录成功后的欢迎界面和登录失败时的提示信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们先来看看效果图:


下面我们看看代码:

首先是main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >


        <ImageButton
            android:id="@+id/img_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />


    </LinearLayout>
    <LinearLayout 
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="账号:"
            android:layout_weight="3"
            android:textColor="#FF0000"
            android:textSize="26sp"
            />
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_zh"
            android:layout_weight="1"
            android:textColor="#00FF00"
            android:textSize="26sp"
            />
    </LinearLayout>
    <LinearLayout 
         android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        >
        <TextView 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="密码:"
            android:layout_weight="3"
            android:textColor="#FF0000"
            android:textSize="26sp"
            />
        <EditText 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_mima"
            android:layout_weight="1"
            android:textColor="#00FF00"
            android:textSize="26sp"
            android:password="true"
            />
    </LinearLayout>
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb_mima"
        android:text="记住密码"
        android:textColor="#000000"
        
        />
    <CheckBox 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/cb_auto"
        android:text="自动登录"
        android:textColor="#000000"
        />
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btn_login"
        android:text="登录"
        android:textColor="#0000FF"
        android:gravity="center"
        android:textSize="26sp"
        />


</LinearLayout>

然后是logo.xml文件用于布局进度条和取消代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <RelativeLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="3"
        >
        <ProgressBar 
            android:id="@+id/pgBar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            />
        <TextView 
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/pgBar"
            android:layout_centerInParent="true"
            android:text="正在登录···"
            android:textColor="#000000"
            android:textSize="18sp"
            />
    </RelativeLayout>
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical"
        >
        <Button 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/btn_back"
            android:text="取消"
            android:textColor="#000000"
            android:textSize="18sp"
            />
    </LinearLayout>


</LinearLayout>

接下来是wel.xml文件,用于实现欢迎界面:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="登陆成功,欢迎进入用户界面!!!"
        android:textColor="#000000"
        android:textSize="20sp"
        />


</LinearLayout>

接下来是MainActivity.java代码,用于设置登陆账号,密码等,实现账号登陆功能:

package com.example.denglu;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;




public class MainActivity extends Activity {

private EditText userName,password;
private CheckBox rem_pw,auto_login;
private Button btn_login;
private ImageButton btnQuit;
private String userNameValue,passwordValue;
private SharedPreferences sp;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main);
        
        sp=this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE);
        userName=(EditText)findViewById(R.id.et_zh);
        password=(EditText)findViewById(R.id.et_mima);
        rem_pw=(CheckBox)findViewById(R.id.cb_mima);
        auto_login=(CheckBox)findViewById(R.id.cb_auto);
        btn_login=(Button)findViewById(R.id.btn_login);
        btnQuit=(ImageButton)findViewById(R.id.img_btn);
        if(sp.getBoolean("ISCHECK", false)){
        rem_pw.setChecked(true);
        userName.setText(sp.getString("USER_NAME", ""));
        password.setText(sp.getString("PASSWORD", ""));
        if(sp.getBoolean("AUTO_ISCHECK", false)){
        auto_login.setChecked(true);
        Intent intent=new Intent(MainActivity.this, LogoActivity.class);
        MainActivity.this.startActivity(intent);
       
        }
        }
        btn_login.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
userNameValue=userName.getText().toString();
passwordValue=password.getText().toString();
if(userNameValue.equals("lovemy_baby")&&passwordValue.equals("ilovexwq3344")){
Toast.makeText(MainActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
if(rem_pw.isChecked()){
Editor editor=sp.edit();
editor.putString("USER_NAME", userNameValue);
editor.putString("PASSWORD", passwordValue);
editor.commit();
}
Intent intent=new Intent(MainActivity.this, LogoActivity.class);
MainActivity.this.startActivity(intent);
}else {
Toast.makeText(MainActivity.this, "用户名或密码错误,请重新登陆", Toast.LENGTH_SHORT).show();
}

}
});
        rem_pw.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(rem_pw.isChecked()){
System.out.println("记住密码已选中");
sp.edit().putBoolean("ISCHECK", true).commit();
}else {
System.out.println("记住密码没有选中");
sp.edit().putBoolean("ISCHECK", false).commit();
}

}
});
        auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(auto_login.isChecked()){
System.out.println("自动登陆已选中");
sp.edit().putBoolean("AUTO_ISCHECK", true).commit();

}else {
System.out.println("自动登陆没有选中");
sp.edit().putBoolean("AUTO_ISCHECK", false).commit();

}

}
});
        btnQuit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
finish();

}
});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

接下来是LogoActivity.java代码:

package com.example.denglu;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ProgressBar;


public class LogoActivity extends Activity{

private ProgressBar progressBar;
private Button button;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.logo);

progressBar=(ProgressBar)findViewById(R.id.pgBar);
button=(Button)findViewById(R.id.btn_back);
Intent intent=new Intent(this, WelActivity.class);
button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
finish();

}
});
}


}

接下来是欢迎界面WelActivity.java代码:

package com.example.denglu;


import android.app.Activity;
import android.os.Bundle;


public class WelActivity extends Activity{


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wel);
}


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值