Android界面布局练习

Android界面布局练习

一、实验目的

  1. 掌握常用的几种界面布局方法。
  2. 能够熟练综合应用各种布局方法进行界面设计。

二、实验内容

  1. 制作如下图所示的手机QQ登陆界面。

在这里插入图片描述

给控件绑定监听器,当用户点击登陆按钮时,把用户所填写的注册内容显示在“注册”按钮下面的文本框内。

2、课本上其它示例程序。
三、预备知识

四、实验步骤
1、新建一个Android项目,项目名称中须包含学号后三位,如“Project101”,否则作业无效。
2、综合利用所掌握的布局方法,实现如图1所示的手机QQ登陆界面。
3、 可以使用的资源图片:
在这里插入图片描述
4、不固定布局方法。

展示

在这里插入图片描述

代码

布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/qq" />
    </LinearLayout>


    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:stretchColumns="0,1">

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/name" />

            <EditText
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/pwd" />

            <EditText
                android:id="@+id/text2"
                android:inputType="textPassword"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>

        <TableRow>

            <RadioGroup
                android:id="@+id/sex"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/sex1"
                    android:checked="true"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sex1" />

                <RadioButton
                    android:id="@+id/sex2"

                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/sex2" />
            </RadioGroup>

            <TextView />


        </TableRow>

        <TableRow>

            <CheckBox
                android:id="@+id/check1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/ji" />

            <CheckBox
                android:id="@+id/check2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/zi" />
        </TableRow>

        <TableRow>

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/zhu" />

            <Button
                android:id="@+id/deng"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="submit"
                android:text="@string/deng" />
        </TableRow>

        <TableRow>

            <TextView
                android:id="@+id/text3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
        </TableRow>
    </TableLayout>


</LinearLayout>

strings.xml

<resources>
    <string name="app_name">project047</string>
    <string name="name">用户名</string>
    <string name="pwd">密码</string>
    <string name="sex1"></string>
    <string name="size">20dp</string>
    <string name="sex2"></string>
    <string name="ji">记住密码</string>
    <string name="zi">自动登陆</string>
    <string name="deng">登陆</string>
    <string name="zhu">注册</string>
</resources>

Main_Activity.java

package com.example.project047;

import android.support.v7.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.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private String text1;
    private String text2;
    private String text3;

    private String sex;
    private String check1;
    private String check2;

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


    }

    public void submit(View view) {

        EditText editText1 = findViewById(R.id.text1);
        EditText editText2 = findViewById(R.id.text2);
        RadioGroup radioGroup = findViewById(R.id.sex);

//        radioGroup.getCheckedRadioButtonId();
//        radioGroup.setOnCheckedChangeListener((group, checkId) -> {
//            sex = checkId == R.id.sex1 ? "男" : "女";
//        });
        int a = radioGroup.getCheckedRadioButtonId();
        if (a == R.id.sex1) {
            sex = "男";
        } else {
            sex = "女";
        }


        /** System.out.println("a"+a);
         *  System.out.println("r"+R.id.sex1);
         * I/System.out: a2131165300
         *     r2131165300
         */
        CheckBox checkBox1 = findViewById(R.id.check1);
        CheckBox checkBox2 = findViewById(R.id.check2);
        check1 = checkBox1.isChecked() ? "是" : "否";
        check2 = checkBox2.isChecked() ? "是" : "否";

        TextView textView = findViewById(R.id.text3);

        text1 = String.valueOf(editText1.getText());
        text2 = String.valueOf(editText2.getText());


        text3 = "用户名:" + text1 + "\n" + "密码:" + text2 + "\n" + "性别:" + sex + "\n"
                + "记住密码:" + check1 + "\n自动登陆:" + check2;

        textView.setText(text3);

    }
}

  • 1
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值