欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android开发学习(4)简单登录界面

程序员文章站 2022-06-10 13:44:45
...

在简单的Hello Android 的基础上,我们进一步学习如果制作一个简单的登录界面(不涉及网络交互,用户名密码先固定为admin ),在这里我们学习获取用户名密码,多个Activity之间跳转。

layout login.xml

先看下成果:
Android开发学习(4)简单登录界面
源码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout          xmlns:android="http://schemas.android.com/apk/res/android"
                         android:layout_width="match_parent"
                         android:layout_height="match_parent"
                         android:background="#2197db"
>

    <TextView
            android:text="XS-TestApp"
            android:id="@+id/loginbutton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="90dp"
            android:layout_marginBottom="20dp"
            android:textColor="#fffefe"
            android:textSize="30sp"/>

    <LinearLayout
            android:id="@+id/input"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/loginbutton"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"
            android:background="#fff"
            android:orientation="vertical">
        <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="44dp"
                android:background="#fff"
                android:gravity="center_vertical"
                android:orientation="horizontal" >

            <EditText
                    android:id="@+id/userId"
                    android:layout_width="wrap_content"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:background="@null"
                    android:imeOptions="actionDone"
                    android:textSize="16sp"
                    android:ems="10"
                    android:hint="请输入用户名"
            >
            </EditText>
            <Button
                    android:id="@+id/button_bar"
                    android:layout_width="20dp"
                    android:layout_height="20dp"
                    android:layout_marginRight="8dp"
                    android:layout_marginLeft="1dp"
                    android:background="@drawable/login_input_arrow"
            />

        </LinearLayout>
        <View
                android:layout_width="fill_parent"
                android:layout_height="1.0px"
                android:layout_marginLeft="1.0px"
                android:layout_marginRight="1.0px"
                android:background="#ffc0c3c4" />
        <EditText
                android:id="@+id/pass"
                android:layout_width="fill_parent"
                android:layout_height="44.0dip"
                android:background="#00ffffff"
                android:gravity="center_vertical"
                android:inputType="textPassword"
                android:maxLength="16"
                android:maxLines="1"
                android:textColor="#ff1d1d1d"
                android:textColorHint="#ff666666"
                android:textSize="16.0sp"
                android:hint="请输入密码"
        />
    </LinearLayout>
    <Button
            android:id="@+id/loginBtn"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/input"
            android:layout_marginTop="10dp"
            android:background="#3aadfd"
            android:text="登  录"
            android:textColor="#ffffff"
            android:textSize="18dp"
            android:layout_centerHorizontal="true"
            android:layout_marginLeft="28dp"
            android:layout_marginRight="28dp"/>
    <TextView
            android:text=""
            android:layout_width="wrap_content"
            android:layout_below="@+id/loginBtn"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:id="@+id/promptText"
            android:textColor="#ff0000"
            android:layout_marginTop="10dp"
            android:textSize="18sp"/>

</RelativeLayout>

LoginActivity

这个类的设计,在初始化的时候获取各个部件,绑定登录按钮操作,在登录按钮触发时校验登录名密码,跳转测试页
源码:

package com.markmoney.xvshu.markmoney.Activitys;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.markmoney.xvshu.markmoney.HelloActivity;
import com.markmoney.xvshu.markmoney.R;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Response;
import com.zhy.http.okhttp.OkHttpUtils;
import org.json.JSONObject;

import java.util.concurrent.TimeUnit;

/**
 * Created by xvshu on 2017/8/7.
 */
public class LoginActivity extends Activity implements View.OnClickListener {
    private static final String TAG = "login";
    Button loginBtn = null;
    EditText useridEt = null;
    EditText passEt = null;
    TextView promptText = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
        loginBtn = (Button) findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(this);
        useridEt = (EditText) findViewById(R.id.userId);
        passEt = (EditText) findViewById(R.id.pass);
        promptText = (TextView) findViewById(R.id.promptText);

    }


    @Override
    public void onClick(View v) {
        String userid = useridEt.getText().toString().trim();
        String pass = passEt.getText().toString().trim();
        if(userid.equals("")){
            promptText.setText(R.string.userIdError);
            return ;
        }
        if(pass.equals("")){
            promptText.setText(R.string.passError);
            return ;
        }


        if (userid.equals("admin") && pass.equals("admin")) {
            Toast.makeText(this, R.string.loginSuccess, Toast.LENGTH_LONG).show();
            Intent intent_hello = new Intent(this, HelloActivity.class);
            startActivity(intent_hello);
            LoginActivity.this.finish();
        } else {
            Toast.makeText(this, R.string.loginError, Toast.LENGTH_LONG).show();
        }

    }
}

AndroidManifest.xml

主文件中,我们改变默认Activity为LoginActivity ,添加测试页Activity

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.markmoney.xvshu.markmoney">

    <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
        <activity
                android:name=".Activitys.LoginActivity"
                android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
                android:name=".HelloActivity"
                android:label="@string/app_name" >
        </activity>

        <service android:name=".service.HelloService" />

    </application>


</manifest>

strings.xml

添加登录相关提示

<resources>
    <string name="app_name">XV-TestApp</string>

    <!-- Strings related to login -->
    <string name="prompt_email">Email</string>
    <string name="prompt_password">Password (optional)</string>
    <string name="action_sign_in">Sign in or register</string>
    <string name="action_sign_in_short">Sign in</string>
    <string name="error_invalid_email">This email address is invalid</string>
    <string name="error_invalid_password">This password is too short</string>
    <string name="error_incorrect_password">This password is incorrect</string>
    <string name="error_field_required">This field is required</string>
    <string name="permission_rationale">"Contacts permissions are needed for providing email completions."</string>

    <string name="default_message">"hello xvshu!"</string>
    <string name="button_send">"say hello"</string>

    <string name="interact_message">You just clicked on the Button!</string>

    <string name="userIdError">You userid is error!</string>
    <string name="passError">You  pass is error!</string>
    <string name="loginSuccess">Login success </string>
    <string name="loginError">Login error </string>




</resources>

成果:
1. 登录成功
Android开发学习(4)简单登录界面
2. 登录失败
Android开发学习(4)简单登录界面

总结

我们发现,安卓的编码实际并不困难,和我们的普通java程序是一样的,入门成本非常低,这也方便了我们这些迫切需要了解的同学,希望在不断学习这个路上,我们能一直同行。

相关标签: android