@vensli
2018-10-08T12:39:11.000000Z
字数 4159
阅读 1321
课程名称 | 手机平台应用开发 | 任课老师 | 郑贵锋 |
---|---|---|---|
年级 | 2016级 | 专业(方向) | 软件工程 |
学号 | 16340275 | 姓名 | 杨赞 |
电话 | 15626405912 | 269088185@qq.com | |
开始日期 | 9月26日 | 完成日期 | 10月6日 |
实现一个Android应用,界面呈现如图中的效果。
要求
整体界面
设置标题,使用TextView控件,textSize为20sp,设置居中ayout_gravity="center_horizontal",layout_marginTop为 20sp。
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:text="中山大学智慧健康服务平台"
android:textAppearance="@android:style/TextAppearance"
android:textSize="20sp"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
设置图片。首先需要添加图片的资源,在res的drawable 文件夹中添加所需的图片。使用imageview控件,选取之前添加的图片。布局方法和之前类似。
四个单选按钮需要一个RadioGroup,将四个RadioButton分别写入RadioGroup中,设置margin为10sp,默认选中设置checkedButton为想要选中的按钮的id
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:checkedButton="@+id/radioButton"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText2">
要求
首先我们考虑button的事件处理,我所运用的方法是给button设置监听器。首先我们要找到需要添加监听的button。
Button button = (Button) findViewById(R.id.button);
之后再给其添加监听器
button.setOnClickListener(new View.OnClickListener()
添加完监听器后我们要在其内部处理事件。题目的要求是当搜索内容为空时弹出toast消息。为了判断搜索框的内容,首先我们要获取搜索框中的文字。
final EditText searchContent = (EditText) findViewById(R.id.editText2);
之后使用Toast.makeText函数来弹出消息。
3.如果搜索成功,则弹出对话框。首先新建对话框
final AlertDialog dialog = new AlertDialog.Builder(this).create()
标题,通过 setTitle()方法设置;
图标,通过 setIcon()方法设置;
显示在中间的主要信息,通过setMessage()方法显示,等等。
如果想要设置对话框中的按钮,使用 dialog.setButton来设置按钮事件。
dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();//关闭对话框
}
});
搜索失败的设置和以上类似。
4. 还有一个要求是RadioButton选择项切换,原理是类似的。先找到radiogroup的id,然后使用setOnCheckedChangeListener函数来设置事件。弹出toast的方法和如上类似就不再详谈了。
1.不知道哪里设置事件
解决思路:设置监听器setOnClickListener
2.获取不了对应button
解决思路:使用findViewById获取id,使用强制转换获取button
3.无法判断搜索文字是否正确
解决思路:使用equal函数判断字符串是否相等