本文用android studio制作了简单的手机qq登录界面,其中界面的布局采用了线性布局、表格布局(不固定布局方法),并给控件绑定监听器,当用户点击登陆按钮时,把用户所填写的注册内容显示在“注册”按钮下面的文本框内。
实现的效果图:
代码:
package com.example.project309; 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.radiobutton; import android.widget.textview; public class mainactivity extends appcompatactivity { edittext name; edittext password; radiobutton man; radiobutton woman; button show; textview shower; checkbox auto; checkbox remember; string result=""; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); name = findviewbyid(r.id.name); password = findviewbyid(r.id.password); man = findviewbyid(r.id.man); woman = findviewbyid(r.id.woman); show = findviewbyid(r.id.show); shower = findviewbyid(r.id.shower); auto = findviewbyid(r.id.auto); remember = findviewbyid(r.id.remember); show.setonclicklistener(this::onclick); } public void onclick(view v){ string user = name.gettext().tostring(); result +="姓名:"+user+"\n"; string pass =password.gettext().tostring(); result +="密码:"+pass+"\n"; if(man.ischecked()){ result+="性别:"+man.gettext().tostring()+"\n"+"设置:"; } if(woman.ischecked()){ result+="性别:"+woman.gettext().tostring()+"\n"+"设置:"; } if(auto.ischecked()){ result+=auto.gettext().tostring()+" "; } if(remember.ischecked()){ result+=remember.gettext().tostring()+" "; } shower.settext(result); } }
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"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <imageview android:layout_width="180dp" android:layout_height="159dp" android:src="@drawable/qq" /> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <tablelayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/table1"> <tablerow> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="用户名" android:textcolor="#000000" android:textsize="20dp" /> <edittext android:id="@+id/name" android:layout_width="150dp" android:layout_height="wrap_content" /> </tablerow> <tablerow> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="密码" android:textcolor="#000000" android:textsize="20dp" /> <edittext android:id="@+id/password" android:layout_width="150dp" android:layout_height="wrap_content" /> </tablerow> <tablerow> <radiobutton android:id="@+id/man" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="男" /> <radiobutton android:id="@+id/woman" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" /> </tablerow> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <checkbox android:id="@+id/auto" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="自动登录" android:textsize="18dp" /> <checkbox android:id="@+id/remember" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="记住密码" android:textsize="18dp" /> </linearlayout> </tablelayout> </linearlayout> </linearlayout> <button android:id="@+id/show" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="注册" android:textsize="20dp" /> <textview android:layout_width="match_parent" android:layout_height="300dp" android:id="@+id/shower"/> </linearlayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。