今日学习

一、美化登陆界面(drawable+background)

二、button控件跳转功能

1.获取edittest中输入的信息

2.与规则匹配

3.成功跳转/失败

 1 package com.example.tempost;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.content.Intent;
 6 import android.os.Bundle;
 7 import android.view.View;
 8 import android.widget.Button;
 9 import android.widget.EditText;
10 
11 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
12 
13     //声明控件
14     private Button myBtnLogin;
15     private EditText myEtUser;
16     private  EditText myEtPassword;
17 
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22 
23         //找到控件
24         myBtnLogin = findViewById(R.id.btn_login);
25         myEtUser = findViewById(R.id.et_1);
26         myEtPassword = findViewById(R.id.et_2);
27 
28 
29 //        //实现跳转---方法一
30 //        myBtnLogin.setOnClickListener(new View.OnClickListener() {
31 //            @Override
32 //            public void onClick(View v) {
33 //                Intent intent = null;
34 //                intent = new Intent(MainActivity.this,FunctionActivity.class);
35 //                startActivity(intent);
36 //            }
37 //        });
38 
39 
40         //方法二---匹配对应的用户名和密码才能登录
41         myBtnLogin.setOnClickListener(this);
42 
43     }
44 
45     public void onClick(View v){
46         //需要获取输入的用户名和密码
47         String username = myEtUser.getText().toString();
48         String password = myEtPassword.getText().toString();
49         Intent intent = null;
50 
51         //假设正确的账号和密码分别是why,123456
52         if(username.equals("why")&&password.equals("123456")){
53             //如果正确的话进行跳转
54             intent = new Intent(MainActivity.this,FunctionActivity.class);
55             startActivity(intent);
56         }else{
57             //不正确,弹出登陆失败
58         }
59     }
60 
61     @Override
62     public void onPointerCaptureChanged(boolean hasCapture) {
63 
64     }
65 }
MainActivity.java
 1 package com.example.tempost;
 2 
 3 import androidx.appcompat.app.AppCompatActivity;
 4 
 5 import android.os.Bundle;
 6 
 7 public class FunctionActivity extends AppCompatActivity {
 8 
 9     @Override
10     protected void onCreate(Bundle savedInstanceState) {
11         super.onCreate(savedInstanceState);
12         setContentView(R.layout.activity_function);
13     }
14 }
FunctionActivity.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context=".FunctionActivity">

    <TextView
        android:id="@+id/tv_func_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="跳转后界面"
        android:textSize="25sp"
        android:gravity="center"
        android:textColor="@color/black"
        />

</LinearLayout>
activity_function.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    android:background="@drawable/background"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/tv_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:text="Welcome"
        android:textColor="#ED6438"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_marginTop="200dp"

    />

    <EditText
        android:id="@+id/et_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textColor="@color/colorBlack"
        android:textSize="16sp"
        android:hint="用户名"
        android:maxLines="1"
        android:padding="5dp"
        android:layout_marginTop="10dp"
        android:background="@drawable/bg_username"
    />

    <EditText
        android:id="@+id/et_2"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:textColor="@color/colorBlack"
        android:textSize="16sp"
        android:hint="密 码"
        android:inputType="textPassword"
        android:maxLines="1"
        android:padding="5dp"
        android:layout_marginTop="5dp"
        android:background="@drawable/bg_username"
    />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="20dp"
        >

        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="登录"
            android:layout_gravity="center"
            android:background="@drawable/btn_left"
        />

        <Button
            android:id="@+id/btn_registered"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:text="注册"
            android:layout_marginLeft="2dp"
            android:background="@drawable/btn_right"
        />

    </LinearLayout>


</LinearLayout>
activity_main.xml

 

posted @ 2021-01-29 15:41  Arisf  阅读(69)  评论(0编辑  收藏  举报