Android高级组件
自动完成文本框(AutoCompleteTextView)
进度条(ProgessBar)
拖动条(SeekBar)
星级评分条(RatingBar)
10.1自动完成文本框(AutoCompleteTextView)
类似于百度的搜索框,使用户输入部分字符后会显示一个下拉菜单,方便用户选择自动填写文本框
要想获取文本,与TextView一样,调用getText()方法并tostring()即可
无数组资源的引用,需要设置适配器
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/autoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:completionHint="请输入你要搜索的内容"
android:completionThreshold="1"
android:text="AutoCompleteTextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" /> </LinearLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final AutoCompleteTextView autoCompleteTextView = findViewById(R.id.autoCompleteTextView);
autoCompleteTextView.setAdapter(new ArrayAdapter(MainActivity.this,android.R.layout.simple_dropdown_item_1line,new String[]{"Java","C#","Android"}));
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("1111","你要搜索的是"+autoCompleteTextView.getText().toString());
}
});
} }
10.2进度条(ProgessBar)
用来提示用户后台是否在处理信息以及处理信息的程度如何
常用方法:
setProgress(int progress);//设置进度完成的百分比
setVisiblility(View.GONE);//设置控件不显示且不占位置
可以通过style属性为ProgressBar指定风格
?android:attr/progressBarStyleHorizontal 细水平长条进度条
?android:attr/progressBarStyleLarge 大圆形进度条
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </LinearLayout>
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar progressBar = findViewById(R.id.progressBar);
final ProgressBar progressBar1 = findViewById(R.id.progressBar1);
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
progressBar1.setProgress(msg.what);
progressBar.setProgress(msg.what);
if(msg.what==100){
progressBar.setVisibility(View.GONE);
}
}
};
new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<=100;i++){
Message message = new Message();
message.what = i;
handler.sendMessage(message);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Log.i("1111",""+i);
if(i==100){
Log.i("1111","已完成");
}
}
}
}).start();
} }
10.3拖动条(SeekBar)
属性:android:thumb
值:Drawable对象
作用:改变外观
监听事件显示在代码中
<SeekBar
android:id="@+id/seekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content" /></LinearLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SeekBar seekBar = findViewById(R.id.seekBar);
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
Log.i("1111",""+i);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}}
10.4星级评分条(RatingBar)
常用方法
getRating();获取星级
getStepSize();获取每次最少改变的星级
getProgress();用于获取进度,值为以上两个之积
<RatingBar
android:id="@+id/ratingBar"
android:isIndicator="false"
android:numStars="5"
android:stepSize="0.5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="提交" /></LinearLayout>
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.RatingBar;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RatingBar ratingBar = findViewById(R.id.ratingBar);
Button button = findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.i("1111",""+ratingBar.getRating()+" "+ratingBar.getStepSize()+" "+ratingBar.getProgress());
}
});
}}