本章将介绍一下内容,均为XML设置UI:
类似于百度的搜索框,使用户输入部分字符后会显示一个下拉菜单,方便用户选择自动填写文本框
属性 | 作用 |
---|---|
android:completionHint | 为弹出的下拉框指定标题 |
android:completionThreshold | 设置用户至少输入的字符个数才会显示下拉框 |
android:text | 显示设置的初始文字与TextView一样 |
<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());
}
});
}
}
用来提示用户后台是否在处理信息以及处理信息的程度如何
属性 | 作用 |
---|---|
android:max | 设置进度条的最大值 |
android:progress | 指定进度条已完成的进度值 |
android:progressDrawable | 设置进度条轨迹绘制形式 |
常用方法:
setProgress(int progress);//设置进度完成的百分比
setVisiblility(View.GONE);//设置控件不显示且不占位置
可以通过style属性为ProgressBar指定风格
?android:attr/progressBarStyleHorizontal 细水平长条进度条
?android:attr/progressBarStyleLarge 大圆形进度条
<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"
android:orientation="vertical"
tools:context=".MainActivity">
<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();
}
}
属性:android:thumb
值:Drawable对象
作用:改变外观
监听事件显示在代码中
<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"
android:orientation="vertical"
tools:context=".MainActivity">
<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) {
}
});
}
}
属性 | 作用 |
---|---|
android:isIndicator | 星级评分条是否可以被改变,true表示不可以变 |
android:numStars | 设置星级评分条总共有几颗星 |
android:rating | 指定默认星级 |
android:stepSize | 设置每次最少改变的星级,默认是半颗星 |
常用方法
getRating();获取星级
getStepSize();获取每次最少改变的星级
getProgress();用于获取进度,值为以上两个之积
<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"
android:orientation="vertical"
tools:context=".MainActivity">
<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());
}
});
}
}