找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2389|回复: 0
打印 上一主题 下一主题
收起左侧

Android布局管理器及简单控件的使用

[复制链接]
跳转到指定楼层
楼主
layout文件夹的布局文件activity_main.xml中设计如下登陆界面
           
(1)需将三个drawable文件,需复制到drawable文件夹。
   editext_selector.xml:编辑框的有无焦点时的边框绘制,引用了下面两个文件。
   shape_edit_focus.xml:编辑框获取焦点时的边框
   shape_edit_normal.xml:编辑框没有获取焦点时的获边框
2)颜色值文件colors.xml,复制到values文件夹。

(注:这些文件一般自己都有,附件是完整实验文件夹,可直接打开使用)

MainActivity.java文件:
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private TextView tv_password;
    private EditText et_password;
    private Button btn_forget;
    private RadioGroup rg_login;
    private CheckBox ck_remember;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //加载布局
        setContentView(R.layout.activity_main);
        //获取控件
        tv_password = findViewById(R.id.textView4);
        et_password = findViewById(R.id.editText3);
        btn_forget = findViewById(R.id.button5);
        ck_remember = findViewById(R.id.checkBox);
        rg_login = findViewById(R.id.radiogroup);
        //单选按钮组绑定监听器
        rg_login.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.radioButton3) {
                    tv_password.setText("登录密码:");
                    et_password.setHint("请输入密码");
                    btn_forget.setText("忘记密码");
                    ck_remember.setVisibility(View.VISIBLE);
                } else if (checkedId == R.id.radioButton4) {
                    tv_password.setText("验证码:");
                    et_password.setHint("请输入验证码");
                    btn_forget.setText("获取验证码");
                    ck_remember.setVisibility(View.INVISIBLE);
                }
            }
        });
    }
}


activity_main.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"
    tools:context=".MainActivity">

    <RadioGroup
        android:id="@+id/radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:orientation="horizontal">

        <RadioButton
            android:id="@+id/radioButton3"
            android:layout_width="197dp"
            android:layout_height="wrap_content"
            android:text="密码登录"
            android:textSize="20sp"
            android:textStyle="bold" />

        <RadioButton
            android:id="@+id/radioButton4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="验证码登录"
            android:textSize="20sp"
            android:textStyle="bold" />
    </RadioGroup>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="120dp"
            android:layout_height="35dp"
            android:text="手机号码:"
            android:textAllCaps="false"
            android:textSize="20sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="294dp"
            android:layout_height="50dp"
            android:background="@drawable/editext_selector"
            android:ems="10"
            android:hint="请输入手机号码"
            android:inputType="phone" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="53dp">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="120dp"
            android:layout_height="35dp"
            android:text="登录密码:"
            android:textSize="20sp"
            android:textStyle="bold" />

        <EditText
            android:id="@+id/editText3"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="@drawable/editext_selector"
            android:ems="10"
            android:hint="请输入密码"
            android:inputType="textPassword" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:text="忘记密码" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="42dp">

        <CheckBox
            android:id="@+id/checkBox"
            android:layout_width="120dp"
            android:layout_height="42dp"
            android:text="记住密码"
            android:textSize="15sp" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <Button
            android:id="@+id/button6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="20sp"
            android:textStyle="bold" />
    </TableRow>

</LinearLayout>
布局管理器及简单控件的使用.rar (8.6 MB, 下载次数: 12)






评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表