安卓开发学习笔记_注册界面编写

安卓开发学习笔记_注册界面编写

效果

点击登录按钮能够获取到页面内容并通过弹出窗的形式提示出来

MainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    private val checkBoxList = ArrayList<CheckBox>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        checkBoxList.add(binding.hobby1)
        checkBoxList.add(binding.hobby2)
        checkBoxList.add(binding.hobby3)
        checkBoxList.add(binding.hobby4)
        checkBoxList.add(binding.hobby5)
        checkBoxList.add(binding.hobby6)
        checkBoxList.add(binding.hobby7)
        checkBoxList.add(binding.hobby8)

        binding.login.setOnClickListener {
            // 用户名
            val user = binding.user.text.toString()
            // 密码
            val pwd = binding.pwd.text.toString()
            // 性别
            val gender : Char
            if(binding.man.isChecked){
                gender = '男'
            }else{
                gender = '女'
            }
            // 兴趣爱好
            val hobby : StringBuilder = StringBuilder()
            for (i in checkBoxList){
                if(i.isChecked){
                    hobby.append("  ${i.text}")
                }
            }
            val outText = "用户名: $user\n密码: $pwd\n性别: $gender\n兴趣爱好:$hobby"
            Toast.makeText(this, outText, Toast.LENGTH_SHORT).show()
        }
    }
}

activity_layout.xml

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="用户名:" />
        <EditText
            android:id="@+id/user"
            android:layout_width="0dp"
            android:layout_weight="6"
            android:layout_height="wrap_content"
            android:hint="请输入用户名"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">

        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="密码:" />
        <EditText
            android:id="@+id/pwd"
            android:layout_width="0dp"
            android:layout_weight="6"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:hint="请输入密码"/>

    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="性别:" />
        <RadioGroup
            android:layout_width="0dp"
            android:layout_weight="6"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton
                android:id="@+id/man"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text="" />

            <RadioButton
                android:id="@+id/woman"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:text=""
                />
        </RadioGroup>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:text="兴趣爱好:"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
        <CheckBox
            android:id="@+id/hobby1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="足球"/>
        <CheckBox
            android:id="@+id/hobby2"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="篮球"/>
        <CheckBox
            android:id="@+id/hobby3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="乒乓球"/>
        <CheckBox
            android:id="@+id/hobby4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="羽毛球"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
        <CheckBox
            android:id="@+id/hobby5"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="排球"/>
        <CheckBox
            android:id="@+id/hobby6"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="画画"/>
        <CheckBox
            android:id="@+id/hobby7"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="跑步"/>
        <CheckBox
            android:id="@+id/hobby8"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="编程"/>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="5dp">
        <Button
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:text="注册"/>
        <Button
            android:id="@+id/login"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:layout_margin="5dp"
            android:text="登录"/>
    </LinearLayout>
</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Y_cen

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值