您的位置:首页 > 移动开发 > Android开发

Android Studio:基本事件处理之Toast、Snackbar、AlertDialog

2017-12-02 15:47 651 查看

一、 实验题目 

事 件 处 理

【目的】

1.了解 Android 编程基础

2.熟悉 ImageView、Button、RadioButton 等基本控件,能够处理这些控件的基本事件

3.学会弹出基本的对话框,能够定制对话框中的内容,能对确定和取消按钮的事件做处理

二、 实现内容

实现一个 Android 应用,界面呈现与上个实验基本一致,要求:



(1)该界面为应用启动后看到的第一个界面

【其中输入学号和密码的控件要用 TextInputLayout 实现】

(2)点击图片,弹出相应的对话框(图略)

(3)切换RadioButton的选项,会弹出相应的Snackbar提示

(4)点击登录按钮依次判断学号是否为空,密码是否为空,用户名和密码是否正确;不正确则给出错误信息,如学号和密码都正确则提示“登录成功”

(5)点击注册按钮,如果切换选项时,RadioButton 选中的是“学生”,那么弹出 Snackbar 信息“学生注册,功能尚未启用”,如果选中的是“教职工”,那么弹出 Toast 信息“教职工注册功能尚未启用”。

三、 实验过程

这次实验是基于上次实验保留的工程文件,布局代码稍作改动,并增加了很多“事件处理”功能。我按实验要求一步步将功能加以实现,过程如下:

(1)应用启动后看到的第一个界面 【使用TextInputLayout控件】

从示例图可以看到,这次app的启动界面和上次的唯一差别便如下:

 

 -------->> 


于是将原来此位置的相关布局控件删除,在build.gradle(Module:app)添加依赖如下:

 


再在activity_main.xml布局文件中加入TextInputLayout控件,每个TextInputLayout里面包含一个EditText。设置好控件位置与text输入格式。

 


 


在MainActivity.java中对其做如下的设置,这样在输入text时左上角会出现浮动标签。

 


在完成此步骤时遇到了一个问题:启动app后,初始界面的EditText已默认被选中。

于是TextInputLayout的画面不是→

,而是→


按网上方法,在EditText的父节点增加了如右代码:


即是将原本的焦点移除了,问题得以解决。

 

(2)点击图片,弹出相应的对话框  

 


弹出的对话框如左。并要求 ↓

1 点击“拍摄”选项,弹出 Toast 信息“您选择了[拍摄]”;

2 点击“从相册选择”选项,弹出 Toast 信息“您选择了 [从相册选择]”;

3 点击“取消”按钮,弹出 Toast 信息“您选择了[取消]”。

在了解AlertDialog和Toast相关知识后,我先在MainActivity.java里创建了一个新的AlertDialog对话框,并在其中放入列表{“拍摄”,“从相册选择”},点击列表选项,判断下标,然后会弹出对应的toast信息。

接着再获取图片id,为其绑定一个监听器,click之后,之前设置的那个AlertDialog便会通过show()将我们所要的对话框展示出来。

 


 

(3)切换RadioButton的选项,会弹出相应的Snackbar提示

在了解Snackbar相关知识后,通过id获取RadioButton,对其监听如下:

 


其中【.setAction】使该 Snackbar 右侧按钮可以被点击并弹出一个Toast信息。【.setActionTextColor(getResources().getColor(R.color.colorPrimary))】是对Action颜色的设定,【 .setDuration(5000)】是对snackbar停留时延的设定(ms)。

最后根据RadioButton的不同显示不同的Snackbar信息。

格式例如: 


 

(4)点击登录按钮依次判断学号是否为空,密码是否为空,用户名和密码是否正确;不正确则给出错误信息,如学号和密码都正确则提示“登录成功”。

【正确的学号和密码分别为“123456”,“6666”】

先是对“登录”Button绑定监听。再通过.getEditText().getText().toString()获取TextInputLayout输入的字符串,对其内容进行判断。若判断“学号/密码为空”则通过对TextInputLayout设置setErrorEnabled(true)和setError("学号/密码不能为空")来弹出相应的提示内容(左下方)。若判断“登录成功/输入错误”,则设置对应的snackbar提示。

 


(部分代码被折叠)

 

(5)点击注册按钮,如果切换选项时,RadioButton选中的是“学生”,那么弹出 Snackbar信息“学生注册,功能尚未启用”,如果选中的是“教职工”,那么弹出Snackbar信息“教职工注册功能尚未启用”。

如何弹出snackbar信息便不再多说。

这个功能的重点在于怎样判断RadioButton选中的值,我们先是找出两个RadioButton所在的RadioGroup,再通过.getCheckedRadioButtonId()找到被选中的那个单选按钮的值。通过判断这个值来设置弹出对应的snackbar信息。

 


(部分代码被折叠)

 

至此,所有“事件处理”功能实现完毕。

将工程导入安卓手机,逐一检查每一功能,效果没有出错。

四、 附上代码

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="com.example.yc.sysu.MainActivity">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="中山大学学生信息系统"
android:textSize="20sp"
android:textColor="#000000"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:focusable="true"
android:focusableInTouchMode="true"/>

<ImageView
android:id="@+id/icon"
android:layout_width="104dp"
android:layout_height="104dp"
app:srcCompat="@drawable/sysu"
app:layout_constraintTop_toBottomOf="@+id/title"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />

<android.support.design.widget.TextInputLayout
android:id="@+id/text_userid"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/icon"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="20dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp">

<EditText
android:id="@+id/userid"
android:textColor="#000000"
android:textSize="18sp"
android:paddingTop="0dp"
android:digits="0123456789"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入学号" />
</android.sup
ca17
port.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/text_userpwd"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/text_userid"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="20dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp">

<EditText
android:id="@+id/userpwd"
android:textColor="#000000"
android:textSize="18sp"
android:password="true"
android:paddingTop="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码" />
</android.support.design.widget.TextInputLayout>

<RadioGroup
android:id="@+id/radioButton"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_userpwd"
android:layout_marginTop="30dp">

<RadioButton
android:id="@+id/radioButton1"
android:text="学生"
android:textColor="#000000"
android:textSize="18sp"
android:checked="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<RadioButton
android:id="@+id/radioButton2"
android:text="教职工"
android:textColor="#000000"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"/>
</RadioGroup>

<View
android:id="@+id/button_box"
android:layout_height="50dp"
android:layout_width="185dp"
app:layout_constraintTop_toBottomOf="@+id/radioButton"
android:layout_marginTop="20dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>

<Button
android:id="@+id/button1"
android:text="登录"
android:textColor="#ffffff"
android:background="@drawable/shape"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="@+id/button_box"
app:layout_constraintTop_toTopOf="@+id/button_box" />

<Button
android:id="@+id/button2"
android:text="注册"
android:textColor="#ffffff"
android:background="@drawable/shape"
android:textSize="18sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintLeft_toRightOf="@+id/button1"
android:layout_marginLeft="10dp"
app:layout_constraintTop_toTopOf="@+id/button_box"
android:layout_marginStart="10dp" />

</android.support.constraint.ConstraintLayout>


MainActivity.java
package com.example.yc.sysu;

import android.content.DialogInterface;
import android.support.design.widget.Snackbar;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
setContentView(R.layout.activity_main);

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
alertDialog.setTitle("上传头像")
.setItems(new String[] {"拍摄","从相册选择"}, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int which) {
if (which == 0)
Toast.makeText(getApplicationContext(), "您选择了[拍摄]",Toast.LENGTH_SHORT).show();
else
Toast.makeText(getApplicationContext(), "您选择了[从相册选择]",Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "您选择了[取消]",Toast.LENGTH_SHORT).show();
}
}).create();

ImageView mImage = (ImageView) findViewById(R.id.icon);
mImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.show();
}
});

final TextInputLayout idText = (TextInputLayout) findViewById(R.id.text_userid);
idText.setHint("请输入学号");
final TextInputLayout pwdText = (TextInputLayout) findViewById(R.id.text_userpwd);
pwdText.setHint("请输入密码");

Button login = (Button) findViewById(R.id.button1);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
idText.setErrorEnabled(false);
pwdText.setErrorEnabled(false);
String id = idText.getEditText().getText().toString();
String pwd = pwdText.getEditText().getText().toString();
idText.setErrorEnabled(true);
pwdText.setErrorEnabled(true);

if (id.equals("")) {
idText.setError("学号不能为空");
return;
}
else if (pwd.equals("")) {
pwdText.setError("密码不能为空");
return;
}
else if (id.equals("123456") && pwd.equals("6666")) {
Snackbar.make(getWindow().getDecorView(), "登录成功",Snackbar.LENGTH_SHORT)
.setAction("确定", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Snackbar的确定按钮被点击了",Toast.LENGTH_SHORT).show();
}
})
.setActionTextColor(getResources().getColor(R.color.colorPrimary))
.setDuration(5000)
.show();
}
else {
Snackbar.make(getWindow().getDecorView(), "学号或密码错误",Snackbar.LENGTH_SHORT)
.setAction("确定", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Snackbar的确定按钮被点击了",Toast.LENGTH_SHORT).show();
}
})
.setActionTextColor(getResources().getColor(R.color.colorPrimary))
.setDuration(5000)
.show();
}
}
});

RadioButton button1 = (RadioButton) findViewById(R.id.radioButton1);
RadioButton button2 = (RadioButton) findViewById(R.id.radioButton2);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(getWindow().getDecorView(), "您选择了学生",Snackbar.LENGTH_SHORT)
.setAction("确定", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Snackbar的确定按钮被点击了",Toast.LENGTH_SHORT).show();
}
})
.setActionTextColor(getResources().getColor(R.color.colorPrimary))
.setDuration(5000)
.show();
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(getWindow().getDecorView(), "您选择了教职工",Snackbar.LENGTH_SHORT)
.setAction("确定", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Snackbar的确定按钮被点击了",Toast.LENGTH_SHORT).show();
}
})
.setActionTextColor(getResources().getColor(R.color.colorPrimary))
.setDuration(5000)
.show();
}
});

Button logup = (Button) findViewById(R.id.button2);
logup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioButton);
RadioButton checked_button = (RadioButton) findViewById(radioGroup.getCheckedRadioButtonId());
String text = checked_button.getText().toString();
if (text.equals("学生")) {
Snackbar.make(getWindow().getDecorView(), "学生注册功能尚未启用", Snackbar.LENGTH_SHORT)
.setAction("确定", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Snackbar的确定按钮被点击了", Toast.LENGTH_SHORT).show();
}
})
.setActionTextColor(getResources().getColor(R.color.colorPrimary))
.setDuration(5000)
.show();
}
if (text.equals("教职工")) {
Snackbar.make(getWindow().getDecorView(), "教职工注册功能尚未启用", Snackbar.LENGTH_SHORT)
.setAction("确定", new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Snackbar的确定按钮被点击了", Toast.LENGTH_SHORT).show();
}
})
.setActionTextColor(getResources().getColor(R.color.colorPrimary))
.setDuration(5000)
.show();
}
}
});

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息