View 控件
处理文本内容的View(TextView)
能对长文本进行显示处理
支持html代码
内容有样式链接效果
android:textSize=""大小
android:textColor=""颜色
android:lineSpacingMultiplier=""行倍距
android:lineSpacingExtra=""行距
android:lines=""行数
android:singleLine="true"单行
android:ellipsize=""start,middle,end,省略号位置,(或跑马灯marquee搭配
android:focusable="true" 设置可以获取焦点 android:focusableInTouchMode="true"设置在触摸时获取焦点 android:marqueeRepeatLimit="marquee_forever" 设置跑马灯持续运行
)
处理图片内容的View(ImageView)
常用属性:
android:src指定前景图片资源无论所处控件长宽大小自动保持原有的比例!
(<ImageButton/>)按钮图片
android:background背景
资源文件夹res/mipmap,res/drawable
接受用户输入的View(EditText)
android:inputType输入类型要输入多种类型数据用|即可
android:hint提示点击开始输入时消失
android:maxLength
进度条类的View(ProgressBar)
默认为圆形没有有刻度只会不断旋转通过设置style显示传统的水平带刻度进度条
style="?android:attr/progressBarStyleHorizontal(水平进度条)"风格
android:indeterminate="true"
永恒滚动 android:progress="30"进度 android:max="200"总体,默认为100
package com.example.mydemo3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;
public class ProgressBarActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_progress_bar);
ProgressBar pb=findViewById(R.id.progress);
//Android中4.0以后不能直接在线程中操作控件会崩溃
//进度条特例
new Thread(){
public void run(){
for (int i=0;i<100;i++) {
pb.setProgress(i);
try {//睡眠
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
<?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=".ProgressBarActivity">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:progress="30"
android:max="200"/>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="true"/>
<ProgressBar
android:id="@+id/progress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="?android:attr/progressBarStyleHorizontal" />
</LinearLayout>
被点击的View(Button)事件监听
解决按钮无法变色的问题在res/values/themes中
<style name="Theme.StartMyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
末尾加.Bridge即可解决
package com.example.mydemo3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
//第三种
public class BUttonActivity extends AppCompatActivity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
Button btn1=findViewById(R.id.btn1);
btn1.setOnClickListener(new MyOnClick());
//method2
Button btn2=findViewById(R.id.btn2);//匿名内部类
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("tag","匿名内部类");
}
});
Button btn3=findViewById(R.id.btn3);
btn3.setOnClickListener(this);
}
//第一种
class MyOnClick implements View.OnClickListener{
@Override
public void onClick(View view) {
Log.e("tag","刚刚注册了内部类");
}
}
@Override//第三种
public void onClick(View view) {
Log.e("tag","当前activity本类实现OnClickListener");
}
//第四种
public void bangding(View view){
switch (view.getId()){
case R.id.btn4: Log.e("tag","xml1绑定的");break;
case R.id.btn5: Log.e("tag","xml2绑定的");break;
};
}
}
<?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=".BUttonActivity"
android:orientation="vertical"
android:layout_marginVertical="20dp"
android:layout_marginHorizontal="50dp">
<Button
android:id="@+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="自定义内部类"
android:textSize="30sp"
android:layout_marginVertical="20dp"/>
<Button
android:id="@+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="匿名内部类"
android:textSize="30sp"
android:layout_marginVertical="20dp"/>
<Button
android:id="@+id/btn3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="当前Activity"
android:textSize="30sp"
android:layout_marginVertical="20dp"/>
<Button
android:id="@+id/btn4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="xml1绑定"
android:textSize="30sp"
android:layout_marginVertical="20dp"
android:onClick="bangding"/>
<Button
android:id="@+id/btn5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="xml2绑定"
android:textSize="30sp"
android:layout_marginVertical="20dp"
android:onClick="bangding"/>
</LinearLayout>
CheckBox
1.系统封装的复选控件
2.两种状态:选中,未选中setChecked()true选中,isChecked()返回是否被选中;
3.监听是否改变setOnCheckedChangeListener()变化就进入匿名内部类
RadioButton
1.单选控件(选择题)
2.和RadioGroup一起使用,只能选择一个
3.选中后再次点击无法被取消大多数UI框架中为圆形CheckBox为方形
ToggleButton
1.android :textOn打开文字android :textoff关闭文字
2.setChecked(boolean)设置是否被打开
3.setOnCheckedChangeListener监听
SeekBar
1.显示当前进度
2.setprogress设置进度
3.setOnSeekBarChangeListener()监听事件