实验一:界面设计:基本组件
一、 实验目的
1. 掌握常用组件在布局文件中的设置。
2. 掌握在 Java 程序中获取组件值
3. 掌握对组件值的验证
4. 掌握基本组件常用的监听器,和事件处理
5. 掌握将组件值提交到下一个 Activity 活动的方法
二、 实验内容
简要介绍本次实验的内容及要求。(通过文字与图片说明)
根据本例,再设计一个问卷调查 UI 界面,并实现在 Java 程序中获取数据、验证数据,以及提交数据到下一个 Activity 页面的功能。
三、 理论
根据实验内容及要求,将涉及到的相关理论方面的知识点在此处列举一下。
Android 通过widget 包提供了各种UI组件,包括按钮、列表框、单选按钮、复选按钮、进度条、图片、日期选择器、时间选择器等基本组件,以及进度条、拖动条、评分条选项卡、图像切换器等高级组件。各个组件具有一些通用的属性,但需要注意的是各组件特有属性的应用。图形界面应用都是通过事件来实现人机交互,Android 中主要包含键盘事件和触摸事件,键盘事件是指用户使用物理键盘过程中产生的事件,如按下、弹起等;而触摸事件是指用户触摸屏生的事件,如按下、弹起、滑动、双击等。同时 Android 中还提供了消息提示框、状态栏上通知、对话框等用户提示方式,需要用户掌握。
四、 设计步骤
将设计内容细化为具体步骤,完善每步的具体内容。
- 设计UI界面
(1) 将主布局修改为线性布局LinearLayout,垂直排列。
(2) 在主布局中添加用户名文本框和输入框。
(3) 在主布局中添加密码文本框和输入框。
(4) 在主布局中添加性别文本框和复选框。
(5) 在主布局中添加联系电话文本框和输入框,设置输入类型为android:inputType = “text|phone”。
(6) 在主布局中添加部门文本框和列表框,设置列表框数据来源为depts.xml文件。
(7) 在主布局中添加爱好文本框和一个线性布局,在该布局中放置四个CheckBox组件,分别表示书籍、运动、音乐和电影。
(8) 在主布局中最后添加一个确定按钮,设置android:onClick属性值为myclick,即当单击确定按钮时,会调用myclick方法。 - 表示部门的Spinner组件,其数据来源文件depts.xml位于res/values目录下,代码如下所示。
- 设计后台程序
(1) 在主Activity文件中,定义布局中的各组件对象和一个存放爱好中各复选框对象的favs动态数组。
(2) 在onCreate()方法中,获取各组件。
(3) 获取性别方法。
(4) 获取爱好方法,爱好以逗号分隔。
(5) 当检查通过时,输出注册信息,提交到下一个Activity页面。
(6) 创建一个result_activity.xml布局文件,放置一个文本框组件,并创建ResultActivity类,修改onCreate()方法如下,显示上页传来的数据。
五、 效果图展示
根据实验内容的要求,把设计的每部分效果图进行展示,并对核心代码进行分析。(此处需要截图)
代码块
各个文件创建位置
activity_main
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textSize="20dp"
/>
<EditText
android:id="@+id/name"
android:layout_width="400dp"
android:layout_height="50dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码"
android:textSize="20dp"
/>
<EditText
android:id="@+id/password"
android:layout_width="400dp"
android:layout_height="50dp"
android:inputType="textPassword"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别"
android:textSize="20dp"
/>
<RadioGroup
android:id="@+id/sex"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="@+id/man"
android:text="男"
android:textSize="20dp"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="50dp"
android:id="@+id/woman"
android:text="女"
android:textSize="20dp"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系电话"
android:textSize="20dp"
/>
<EditText
android:id="@+id/tel"
android:layout_width="400sp"
android:layout_height="wrap_content"
android:inputType="text|phone"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="部门"
android:textSize="20dp"
/>
<Spinner
android:id="@+id/dept"
android:layout_width="400dp"
android:layout_height="50dp"
android:entries="@array/dept"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="爱好"
android:textSize="20dp"
/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:orientation="horizontal">
<CheckBox
android:id="@+id/book"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="书籍"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/music"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐"
android:textSize="20dp"/>
<CheckBox
android:id="@+id/movie"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电影"
android:textSize="20dp"/>
</LinearLayout>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="确定"
android:onClick="myclick"
/>
</LinearLayout>
array
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="dept">
<item>人力资源部</item>
<item>销售部</item>
<item>财务部</item>
<item>开发部</item>
</string-array>
</resources>
MainActivity
package com.example.test1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<CheckBox> favs;
private EditText userName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userName = (EditText) findViewById(R.id.name);
favs = new ArrayList<CheckBox>();
CheckBox book = findViewById(R.id.book);
CheckBox sport = findViewById(R.id.sport);
CheckBox music = findViewById(R.id.music);
CheckBox movie = findViewById(R.id.movie);
favs.add(book);
favs.add(sport);
favs.add(music);
favs.add(movie);
}
public String getSex(){
RadioGroup sex = findViewById(R.id.sex);
RadioButton radioButton = (RadioButton) findViewById(sex.getCheckedRadioButtonId());
return radioButton.getText().toString();
}
public String getFavorite() {
String favo="";
for(CheckBox cb:favs) {
if(cb.isChecked()) {
favo += cb.getText().toString();
favo += ",";
}
}
if(!"".equals(favo))
favo=favo.substring (0, favo.length()-1);
else
favo="您未选择爱好!";
return favo;
}
public void myclick(View view) {
if(check()==1) {
StringBuilder sb=new StringBuilder () ;
sb.append("用户名:"+ userName.getText().toString()+"\n");
sb.append("性别:"+getSex()+"\n");
EditText tel = findViewById(R.id.tel);
sb.append("电话:"+tel.getText().toString()+"\n");
Spinner dept = (Spinner) findViewById(R.id.dept);
sb.append("部门:"+dept.getSelectedItem().toString()+"\n") ;
sb.append("爱好:"+getFavorite());
Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
Intent intent = new Intent();//将注册信息提交到ResultActivity页面
intent.setClass(MainActivity.this, ResultActivity.class);
intent.putExtra("info", sb.toString());
MainActivity.this.startActivity(intent) ;
} else {
userName.setError("用户名不能为空");
}
}
//判断用户名是否为空; 0为空,1不为空
private int check() {
String name = userName.getText().toString();
if(name.isEmpty()) {
//Toast.makeText(MainActivity.this,"用户名不能为空",Toast.LENGTH_SHORT).show();
return 0;
}
return 1;
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Test1"
tools:targetApi="31">
<activity
android:name=".ResultActivity"
android:exported="true">
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
result_activity
<?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"
tools:context=".ResultActivity">
<TextView
android:id="@+id/result"
android:layout_width="400dp"
android:layout_height="350dp"
android:text="TextView"
android:textSize="20dp"/>
</LinearLayout>
ResultActivity
package com.example.test1;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class ResultActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.result_activity);
TextView result = (TextView) findViewById(R.id.result);
result.setText("从前一个页面传过来的内容如下:\n\n"+this.getIntent().getStringExtra("info"));
}
}