Android界面布局练习
一、实验目的
- 掌握常用的几种界面布局方法。
- 能够熟练综合应用各种布局方法进行界面设计。
二、实验内容
- 制作如下图所示的手机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);
}
}