Android学习笔记(第一行代码) 做一个简单的登录界面

一.建立xml文件

1.账户输入框和密码输入框的编写

    <TextView
        android:layout_height="wrap_content"
        android:text="@string/LG_name" />
        <EditText
            android:id="@+id/account"
            android:layout_height="wrap_content" 
            android:hint="@string/LG_name_"/>
        </TableRow>
    <TableRow>
    <TextView
        android:layout_height="wrap_content"
        android:text="@string/LG_m" />
        <EditText
            android:id="@+id/password"
            android:layout_height="wrap_content"
            android:inputType="textPassword" />
        </TableRow>
    <TableRow>

android:inputType=”textPassword” 是密码格式,输入后显示星号*。

2.登录按键编写

    <Button
        android:id="@+id/login"
        android:layout_height="wrap_content"
        android:text="@string/LG_button" />

《第一行代码》原文中是

    <Button
        android:id="@+id/login"
        android:layout_height="wrap_content"
        android:layout_span="2"
        android:text="@string/LG_button" />

由于原文布局是TableLayout所以使用了合并单元格的方法让其美观(android:layout_span=”2”合并俩个单元格)

二.登录界面活动的编写

package com.example.ohmst;

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.EditText;
import android.widget.Toast;

public class LoginActivuty extends Activity{

    private EditText accountEdit;
    private EditText passwordEdit;
    private Button login;

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

     accountEdit = (EditText) findViewById(R.id.account);
     passwordEdit = (EditText) findViewById(R.id.password);
     login = (Button) findViewById(R.id.login);

     login.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        String account = accountEdit.getText().toString();
        String password = passwordEdit.getText().toString();
            // 如果账号是admin且密码是123456,就认为登录成功
    if (account.equals("admin") && password.equals("123456")) {
Intent intent = new Intent(LoginActivuty.this,MainActivity.class);
startActivity(intent);
finish();
    } else {
    Toast.makeText(LoginActivuty.this, "登录密码不正确或账户不存在!",
    Toast.LENGTH_SHORT).show();
    }
            }
            }  );

        }
}

注册按键点击事件用getText()采集输入框里的数据

三.对AndroidManifest.xml文件进行配置

<activity
android:name=".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=".MainActivity" >
</activity>

把原来默认第一个启动的 .MainActivity
改为建立的登录活动 .LoginActivity
然后在对 .MainActivity进行注册

以上就可以完成一个及其简单的登陆功能(并没什么实际卵用),原书有更加完善的登录界面的编写,以解决在登陆时所遇到的冲突问题。

扫描二维码关注公众号,回复: 4617490 查看本文章

因为所有的代码的是从书里调出来整理过的,假装是翻译QAQ

猜你喜欢

转载自blog.csdn.net/qq_40712616/article/details/79019911