安卓开发学习笔记_注册界面编写
效果
点击登录按钮能够获取到页面内容并通过弹出窗的形式提示出来
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>