我是靠谱客的博主 满意山水,这篇文章主要介绍Android—高级组件对应属性大全及使用详例AutoCompleteTextViewProgressBarSeekBarRatingBarTabHost,现在分享给大家,希望可以做个参考。

在Android的开发中,用户界面的设计尤其重要,基本的一些组件非常简单,只要有过了解加上操作,就能熟练的运用,但是高级的组件还需要温故知新、查漏补缺。下面是我总结的属性及使用详例,快快收藏叭!

高级UI对应属性大全及使用详例

  • AutoCompleteTextView
  • ProgressBar
  • SeekBar
  • RatingBar
  • TabHost

AutoCompleteTextView

AutoCompleteTextView用于输入一定字符后显示下拉列表框,供用户进行选择

XML属性功能
android:completionHint设置文本框中的提示信息
android:dropDownHeight设置提示框的高度单位dp
android:dropDownWidth设置提示框的宽度单位dp
android:popuPBackground为提示框设置背景
android:completionThreshold设置输入几个字符串才会显示提示信息
其他其他属性不常用,使用默认设置的就好

下面看看详例:
在这里插入图片描述
MXL文件布局:

复制代码
1
2
3
4
5
6
7
8
9
<AutoCompleteTextView android:id="@+id/comtv" android:layout_weight="7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:completionHint="输入查找内容" android:completionThreshold="1" />

Activity类代码,主要是为 AutoCompleteTextView配置一个适配器,具体请看代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; public class MainActivity extends AppCompatActivity { private String[] str={"a","ab","abc","abcd"}; AutoCompleteTextView autoCompleteTextView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); autoCompleteTextView=findViewById(R.id.comtv); //获取自动完成文本框 ArrayAdapter arrayAdapter=new ArrayAdapter(this,android.R.layout.simple_dropdown_item_1line,str); //创建适配器 autoCompleteTextView.setAdapter(arrayAdapter); //加入适配器 } }

ProgressBar

ProgressBar就是我们常用的进度条

XML属性功能
可用 style设置进度条样式
android:max设置进度条最大值
android:progress设置进度条当前进度
android:progressDrawable设置进度条轨道的绘制形式
复制代码
1
2
style
XML属性功能
?android:attr/progressBarStyleHorizontal细长条进度条
?android:attr/progressBarStyleLarge大圆形进度条
?android:attr/progressBarStyleSmall小圆形进度条
@android:style/Widget.ProgressBar.Horizontal粗长条进度条
@android:style/Widget.ProgressBar.Large大跳跃旋转的进度条
@android:style/Widget.ProgressBar.Small大跳跃旋转的进度条

在这里插入图片描述
发挥进度条真正的作用,还需配合进程Thread和Handler使用,以更新进度条,起到提示作用。
添加圆形的进度条

复制代码
1
2
3
4
5
6
7
<ProgressBar android:id="@+id/pbar" style="?android:attr/progressBarStyleLarge" android:layout_width="match_parent" android:layout_height="wrap_content" />

MainActivity代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.widget.ProgressBar; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; /**********************配合进程Thread和Handler对进度条进行更新**********************************************/ public class MainActivity extends AppCompatActivity { int m=0; ProgressBar pbar; private Handler handler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pbar=findViewById(R.id.pbar); handler=new Handler(){ @Override public void handleMessage(@NonNull Message msg) { if (msg.what==0x1121) //0x1121,等于为其命名 // 假如为水平进度条,应在if下为其设置新进度,但我选择的是圆形加载进度条,不用为其设置新进度, Toast.makeText(MainActivity.this,"加载中...",Toast.LENGTH_SHORT).show(); else{ Toast.makeText(MainActivity.this,"加载完成",Toast.LENGTH_LONG).show(); pbar.setVisibility(View.GONE);//关闭进度条释放内存 } } }; new Thread(new Runnable() { @Override public void run() { while (true){ Message message=new Message(); m+= (int) (Math.random()*10); //生成10内的随机数,模拟一个耗时操作 if (m<100) { try { Thread.sleep(1000); //为体现效果,让线程休眠1秒 } catch (InterruptedException e) { e.printStackTrace(); } message.what=0x1121; handler.sendMessage(message); //发送信息 } else { try { Thread.sleep(1000); //为体现效果,让线程休眠1秒 } catch (InterruptedException e) { e.printStackTrace(); } message.what=0x110; handler.sendMessage(message); break; } } } }).start(); } }

SeekBar

SeekBar是拖动条,用户可以进行拖动,用来改变某种数值
为其设置监听后并重写方法
3种常用方法如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import android.os.Bundle; import android.widget.SeekBar; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { SeekBar seekBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); seekBar=(SeekBar) findViewById(R.id.seek); seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int i, boolean b) { //进度改变时执行的方法,i为当前进度值 } @Override public void onStartTrackingTouch(SeekBar seekBar) { //拖动开始时的方法 } @Override public void onStopTrackingTouch(SeekBar seekBar) { //拖动结束时的方法 } }); } }

RatingBar

RatingBar是星状的评分条,如淘宝的评价等

XML属性功能
android:rating设置默认选择的星星数
android:numStars设置评分条总星数
android:stepSize设置评分条最少改变的星星数
android:isIndicator设置评分条是否能改变,true为不能

下面是RatingBar的常用方法与实例
在这里插入图片描述
XML代码

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<RatingBar android:id="@+id/rating" android:layout_width="wrap_content" android:layout_height="wrap_content" android:numStars="5" android:rating="5" android:stepSize="1" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:background="@color/colorAccent" />

MainActivity中主要代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.RatingBar; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { RatingBar rating1; Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rating1=findViewById(R.id.rating); button=findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int a=rating1.getProgress();//获取进度,为getRating()与getStepSize()的乘积 float b=rating1.getRating();//获取获得的星数 float c=rating1.getStepSize();//获取每次要改变的星数 float d=rating1.getNumStars();//获取总星数 Log.i("RatingBar", "进度为:"+a+",获得的星数为:"+b+",每次要改变的星数为:"+c+",评分条总星数为:"+d); } }); } }

TabHost

实现图:
在这里插入图片描述
实现步骤
1、在布局文件中添加TabHost、TabWidget、FrameLayout组件(注意:TabHost、TabWidget、FrameLayout的id需要用系统指定的)
2、编写要显示的xml文件
3、在Activity中初始化TabHost并添加标签页
布局文件代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content" tools:context=".MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="match_parent"/> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent"/> </LinearLayout> </TabHost>

MainActivity中的代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import android.os.Bundle; import android.view.LayoutInflater; import android.widget.TabHost; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { TabHost tabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tabHost=findViewById(android.R.id.tabhost); tabHost.setup(); //初始化Tabhost LayoutInflater inflater= LayoutInflater.from(this); //该对象用于加载标签页 inflater.inflate(R.layout.contacts,tabHost.getTabContentView()); inflater.inflate(R.layout.news,tabHost.getTabContentView()); tabHost.addTab(tabHost.newTabSpec("news").setIndicator("消息").setContent(R.id.new_1)); //添加标签页 setContent(R.id.new_1)为布局文件的id tabHost.addTab(tabHost.newTabSpec("contacts").setIndicator("联系人").setContent(R.id.contacts_1)); } }

最后

以上就是满意山水最近收集整理的关于Android—高级组件对应属性大全及使用详例AutoCompleteTextViewProgressBarSeekBarRatingBarTabHost的全部内容,更多相关Android—高级组件对应属性大全及使用详例AutoCompleteTextViewProgressBarSeekBarRatingBarTabHost内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部