1.基本界面控件
1.1知识回顾
1.TextView:文本控件。addTextChangeListener监听。
2.EditText:输入框。addTextChangeListener监听。
3.Button:按钮。setOnclicListener监听。或者onClick标签。
4.Toast:快捷短消息窗口,没有焦点,马上消失。设置屏幕居中显示(注意版本需要在30以下)。
测试案例:
第一种:监听TextView
-
addTextChangedListener去监听
// 获TextView对象
TextView textView=this.findViewById(R.id.text111);
// 创建监听对象
textView.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
第二种:监听EditText
-
addTextChangedListene,监听的是输入前,输入时和输入后的值
// 获取edit对象
EditText editText=findViewById(R.id.edit1);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
}
});
第三种:监听Button,匿名内部类的方式。
// 获取Button对象
Button button=this.findViewById(R.id.btn1);
// 设置button的监听
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
第四种:监听Button,标签的方式。
-
布局文件的标签中注明onClick
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="点我"
android:onClick="textHandle"
/>
-
Activity中去实现onClick中的同名方法
public void textHandle(View view) {
Toast.makeText(this, "你好", Toast.LENGTH_SHORT).show();
}
1.2重点内容
1.需要继续学习的控件
-
CheckBox:多选框。
-
ImageView:加载图片。
-
RadioGroup:单选框的容器。
-
RadioButton:单选框。
-
ProgressBar:进度条。
-
Spinner:下拉框。
1.3checkBox
1.简介:复选框。可以一次性的选中多个内容。常常跟着多个案例。
2.案例:创建复选框
-
text:复选框要显示出来的文字
-
checked:默认被选中
<!-- 创建复选框-->
<CheckBox
android:id="@+id/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"
/>
<CheckBox
android:id="@+id/check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球"
android:checked="true"
/>
效果图:
3.复选框的两种状态:选中和未被选中。Java如何检测复选框有没有被选中。
-
isChecked():是不是被选中
-
setChecked(布尔值):设置默认的选中状态
-
toggle():切换选中和未被选中的状态
4.如何设置事件的监听?
一个布局文件中可以有多个checkBox,需要给每个checkBox设置一个监听事件。
监听的时候往往是根据复选框有没有被选中的状态进而做出相应的事件的处理机制。
方法一:实现接口
-
实现onCheckedChangeListener接口
-
实现onCheckedChanged()方法
方法二:匿名内部类:
-
setOnCheckedChangeListener
5.案例:事件监听,打印选中的信息。
-
方法一:匿名内部类
设置监听:
CheckBox checkBox=findViewById(R.id.check1);
// 匿名内部类的方式实现监听
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox.isChecked()){
Toast.makeText(CheckboxActivity.this, "我的兴趣爱好是:"+checkBox.getText(), Toast.LENGTH_SHORT).show();
}
}
});
布局文件:
<CheckBox
android:id="@+id/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"
/>
效果图:
点击前:
6.案例2:
效果图:
点击前:
点击后:
参考代码:
第一步:创建布局文件
<!-- 创建复选框-->
<CheckBox
android:id="@+id/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球"
/>
<CheckBox
android:id="@+id/check2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球"
android:checked="true"
/>
<CheckBox
android:id="@+id/check3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="排球"
/>
<CheckBox
android:id="@+id/check4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="羽毛球"
/>
<!-- 点一下按钮,屏幕上显示内容-->
<Button
android:id="@+id/checkbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我显示兴趣爱好"
/>
<!-- 显示内容的区域-->
<TextView
android:id="@+id/textbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
/>
第二步:创建activity事件监听
-
设置复选框的事件监听
-
设置button的事件监听
//复选框的测试checkBox
public class CheckboxActivity extends AppCompatActivity {
String str="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkbox);
// 创建一个字符串变量用于连接
// 获取四个多选
CheckBox checkBox1=findViewById(R.id.check1);
CheckBox checkBox2=findViewById(R.id.check2);
CheckBox checkBox3=findViewById(R.id.check3);
CheckBox checkBox4=findViewById(R.id.check4);
// 获取btn和text
TextView textView=findViewById(R.id.textbtn);
Button button=findViewById(R.id.checkbtn);
// 设置事件的监听
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 显示完重新设置为空
if (str!=null)
textView.setText(str);
str="";
}
});
// 匿名内部类的方式实现监听
// 第一个复选
checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox1.isChecked()){
str=str+checkBox1.getText();
}
}
});
// 第二个复选
checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox2.isChecked()){
str=str+checkBox2.getText();
}
}
});
// 第三个复选
checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox3.isChecked()){
str=str+checkBox3.getText();
}
}
});
// 第四个复选
checkBox4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if (checkBox4.isChecked()){
str=str+checkBox4.getText();
}
}
});
}
}
1.4ImageView
1.简介:负责显示图片的控件。只负责显示图片。
2.常见的属性和属性对应的方法
属性 | 方法 | 说明 |
---|---|---|
adjustViewBounds | setAdjustViewBounds | 自动调整长宽的比例保证显示所需要的内容 |
maxHeight | setMaxHeight | 设置imageView的最大宽度。单独使用无效,必须与adjustViewBounds一起使用。 |
maxWidth | setMaxWidth | 设置imageView的最小宽度。单独使用无效,必须与adjustViewBounds一起使用。 |
src | setImageResource(int) | 设置ImageView要显示的图片 |
alpha | 设置透明度的。0-1之间。 |
当想设置图片的固定宽度和高度的时候,又想保持宽高比怎么办?
将adjustViewBounds设置为true。
将maxWidth和Height设置上固定值。
将layout_height和width设置自适应。
3.案例:点击按钮依次切换一二三张图片。点击按钮调整透明度。
效果图:
图像太大无发上传!
第一步:设置布局文件
全局采用的是线性布局-竖向的
button按钮区域采用的是线性布局-横向的
图片显示区域采用的是帧布局
<?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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".ImageviewActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<!-- 设置按钮-->
<Button
android:padding="1dp"
android:textSize="12dp"
android:id="@+id/btnn1"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="第一张"
/>
<Button
android:padding="1dp"
android:textSize="12dp"
android:id="@+id/btnn2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="第二张"
/>
<Button
android:padding="1dp"
android:textSize="12dp"
android:id="@+id/btnn3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="第三张"
/>
<Button
android:padding="1dp"
android:textSize="12dp"
android:id="@+id/btnn4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="透明度++"
/>
<Button
android:padding="1dp"
android:textSize="12dp"
android:id="@+id/btnn5"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="透明度--"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical"
>
<!-- 图片-->
<ImageView
android:id="@+id/image1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxWidth="300dp"
android:maxHeight="300dp"
android:src="@mipmap/one"
android:layout_gravity="center"
/>
</FrameLayout>
</LinearLayout>
第二步:创建actiity文件,创建监听器
//imageView案例
public class ImageviewActivity extends AppCompatActivity {
// 设置button
Button btn1=null;
Button btn2=null;
Button btn3=null;
Button btn4=null;
Button btn5=null;
float a=1;
// 设置图片
ImageView imageView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_imageview);
btn1=findViewById(R.id.btnn1);
btn2=findViewById(R.id.btnn2);
btn3=findViewById(R.id.btnn3);
btn4=findViewById(R.id.btnn4);
btn5=findViewById(R.id.btnn5);
imageView=findViewById(R.id.image1);
// 设置事件监听
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.mipmap.one);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.mipmap.two);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
imageView.setImageResource(R.mipmap.three);
}
});
// 设置透明度
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (a<1.0){
a= (float) (a+0.1);
imageView.setAlpha(a);
}
}
});
// 设置透明度
btn5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (a>0){
a= (float) (a-0.1);
imageView.setAlpha(a);
}
}
});
}
}
1.5RadioGroup
1.简介:单选按钮。继承的是View,也继承的LinerLayout是一种线性布局。
2.RadioGroup是单选按钮的一个容器,RadioButton是单选按钮。
3.案例:定义一个单选按钮
效果图:
参考代码:
-
android:checkedButton="@id/A"默认选中的单选按钮
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:checkedButton="@id/A"
>
<!-- 设置单选按钮-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择最正确的答案:"
/>
<RadioButton
android:id="@+id/A"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
/>
<RadioButton
android:id="@+id/B"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
/>
</RadioGroup>
扩展:
-
android:layout_gravity="center",设置子元素在父控件中的未知
-
android:gravity="center",View中的元素内容在控件本身中居中显示
4.RadioGroup常用的方法
-
check(id),设置要选中的单选按钮的编号
-
clearCheck(),清空选中状态
-
getCheckedRadioButtonId(),取得选中按钮的RadioButton的id。没有选中的按钮的话返回-1.
-
setOnCheckedChangeListener(),设置单选按钮选中的操作事件。
1.6RadioButton
1.简介:常用的两个方法
-
isChecked(),当前的Radio是不是被选中
-
setChecked(boolean checked),设置默认选中
-
toggle(),切换单选框的选中和没有选中的状态
2.RadioGroup设置监听
-
实现的接口RadioGroup.OnCheckedChangeListener
-
绑定方法为,setOnCheckedChangeListener
3.RadioGroup监听的方法
public void onCheckedChanged(RadioGroup group, int checkedId)
-
参数1,状态发生的RadioGroup
-
参数2,checkedId,当前被选中的RadioButton的id
4.案例:设置RadioGroup的事件监听。
第一种方法:匿名内部类的方式
// 设置事件的监听
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
// 选中的是第一个的话就打印显示信息
if (i==radioButton1.getId()){
Toast toast=Toast.makeText(RadiogroupActivity.this, "当前选中的是A", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
if (i==radioButton2.getId()){
Toast toast=Toast.makeText(RadiogroupActivity.this, "当前选中的是B", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
}
});
效果图:
第二种方法:实现接口的方式
先实现接口:
public class RadiogroupActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
}
再重写方法:
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
}
设置监听:
这句话一定要放到Oncreate方法中,把当前的对象传入进去
radioGroup.setOnCheckedChangeListener(this);
不传入的话,监听器是没有效果的。
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
// 选中的是第一个的话就打印显示信息
if (i==radioButton1.getId()){
Toast toast=Toast.makeText(RadiogroupActivity.this, "当前选中的是A", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
if (i==radioButton2.getId()){
Toast toast=Toast.makeText(RadiogroupActivity.this, "当前选中的是B", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER,0,0);
toast.show();
}
}
1.7ImageButton
1.简介:图片按钮。用以实现能够显示图像功能的控件按钮。
2.Button和Image的区别?
-
主要的区别在于前者有文本后者是显示的图片而不是文本。
3.如何设置ImageButton的图片?
一个标签的形式一个Java文件中设置
-
src属性
-
setImageResource(int)的方法
4.设置背景色?
button和imageButton都是有背景色的,当按钮处于不同的状态的时候背景色也是会随之改变的。一般常常把背景色设置为透明,防止因背景色带来的干扰。
案例:
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/download"
/>
效果图:
-
默认显示的是带背景的。
将背景设置为#0000后:
设置成android:background="@null"也是可以的。
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/download"
android:background="#0000"
/>
效果图:
5.如何设置ImageButton的监听呢?
-
和前面的Button的监听的方法是一样的。
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
}
});
1.8ProgressBar
1.进度条:代表的是程序运行状态的动态控件。表示程序正在进行中。
-
长形的,可以告诉用户的具体的进度
-
圆形的:不能告诉用户具体的进度,但一直处于滚动的状态,适合未知结束时间的场合。
2.样式:
-
长形进度条(progressBarStyleHorizontal)
-
圆形进度条
圆形进度条:
-
大型,progressBarStyleLarge
-
中型
-
小型,progressBarStyleSmall
3.如何设定进度条的样式?
第一种方式:xml属性中的style属性进行配置。
-
引用的是安卓自带的样式属性
style样式中常见的几种:
style="@android:style/Widget.ProgressBar.Small" //小型圆形进度条
style="@android:style/Widget.ProgressBar.Small.Inverse" //小型圆形进度条
style="@android:style/Widget.ProgressBar.Inverse" //中型圆形进度条
style="@android:style/Widget.ProgressBar.Large" //大型圆形进度条
style="@android:style/Widget.ProgressBar.Large.Inverse" //大型圆形进度条
style="@android:style/Widget.ProgressBar.Horizontal" //水平进度条
案例:查看进度条的类型
<!--第一个:条形-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="10"
style="@android:style/Widget.ProgressBar.Horizontal"
/>
<!-- 第二个:小型圆形-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="10"
style="@android:style/Widget.ProgressBar.Small.Inverse"
/>
<!-- 小型圆形-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="10"
style="@android:style/Widget.ProgressBar.Small"
/>
<!-- 种型圆形-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="10"
style="@android:style/Widget.ProgressBar.Inverse"
/>
<!-- 种型圆形-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="10"
style="@android:style/Widget.ProgressBar"
/>
<!-- 大型圆形-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="10"
style="@android:style/Widget.ProgressBar.Large"
/>
<!-- 大型圆形-->
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:progress="10"
style="@android:style/Widget.ProgressBar.Large.Inverse"
/>
效果图:
带有Inverse参数和不带有Inverse参数的style属性区别在于:当进度条控件所在的界面背景颜色为白色时,需要使用带有Inverse参数的style属性,否则进度条将看不见。
4.自定义进度条的样式?
-
xml文件中定义
推荐阅读博客:Android 打造形形色色的进度条 实现可以如此简单_鸿洋_的博客-CSDN博客_android 自定义进度条
5.xml中常用的属性?
-
max:设置进度条的最大值
-
progress:设置缺省的进度值。数值在0到max之间。
-
secondaryProgress:第二的进度值。
-
visibility:设置是不是可见的。
-
View.VISIBLE:设置进度条可见
-
View.INVISIBLE:不可见,但是会占用空间
-
View.GONE:设置进度条不可见,不占布局空间
-