Android 基础UI组件(二)
1、Spinner
提供一个快速的方法来从一组值中选择一个值。在默认状态Spinner显示当前选择的值。触摸Spinner与所有其他可用值显示一个下拉菜单,可以选择一个新的值。
/**
* 写死内容:
* 只需在value内创建一个array,写上数据,在xml里引用
*/ <array name="city">
<item>北京</item>
<item>上海</item>
<item>广州</item>
<item>天津</item>
<item>大连</item>
</array> <Spinner
android:id="@+id/sp_Ct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/city" />
动态设置数据:
sp_name = (Spinner) findViewById(R.id.sp_name);
//通过适配器进行数据得绑定
//方式一:
//创建一个数组适配器(参数:上下文、下拉列表里的布局、显示下拉列表选项的组件ID、数据)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,android.R.id.text1,name);
//方式二:
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,
R.array.city,android.R.layout.simple_dropdown_item_1line);
sp_name.setAdapter(adapter);
2、AutoCompleteTextView
<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:completionThreshold="1"/>
auto = (AutoCompleteTextView) findViewById(R.id.auto);
/**
* AutoCompleteTextView:用法与Spinner类似
* 为了实现自动完成,必须提供建议的文本
* android:completionThreshold这个属性控制匹配几个字符显示列表
*/
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,name);
auto.setAdapter(adapter);
3、ProgressBar
在默写操作进度中的可视指示器,为用户呈现操作的进度,还有一个次要的进度条,用来显示进度,如在流媒体播放的缓冲区的进度
一个进度条也可不显示进度,在不确定模式下,进度条显示循环动画。这种模式常用于应用程序使用任务的长度是未知的进度条也就是一个表示运转的过程,例如发送短信,连接网络等,表示一个过程正在执行中。
两种展示方式:表盘形式(普通、大、小)和进度条填充形式,在layout定义时,需要通过设置style属性类设置展示方式
<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <ProgressBar
android:id="@+id/progressBar"
android:progress="50"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
/*
* xml重要属性:
* android:max 进度条长度最大值
* android:progress 进度条当前进度值
* android:secondaryProgress 第二进度条进度值
* android:progressBarStyle 默认进度条样式
* android:progressBarStyleHorizontal 水平样式
*/
/*
* 重要方法:
* getMax() 返回这个进度条的范围上限
* getProgress() 返回当前进度值
* getSecondaryProgress() 返回次要当前进度之
* incrementProgressBy(int diff) 指定增加的进步--即步长
* isIndeterminate() 指示进度条是否在不确定模式下
* setVisibility(int v) 设置进度条是否可视
*/
创建对话框进度条
//创建对话框进度条
ProgressDialog pd = new ProgressDialog(this);
pd.setMax(100);
// pd.setIndeterminate(false);
pd.setProgress(30);
pd.setSecondaryProgress(70);
//设置是否可以取消
pd.setCancelable(true);
pd.setTitle("下载中");
pd.setMessage("正在下载中。。");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.show();
设置标题进度条(该方法必须在setContentView方法之前)
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
//显示标题栏进度条
setProgressBarIndeterminate(true);
4、Dialog
/**
* 对话框指的是在当前Activity之上的一个小窗口.
* 对话框一般用于提示信息和与当前应用程序直接相关的小功能
* 处于对话框下面的Activity失去焦点 对话框接受所有用户的交互
* @author Administrator
*
*/
//创建一个提示对话框的构造者
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("温馨提示");
dialog.setMessage("1111");
dialog.setIcon(R.mipmap.ic_launcher);
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//选择之后的操作
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//选择之后的操作
}
})
.show();
AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(MainActivity.this);
alertDialog2.setTitle("提示");
alertDialog2.setIcon(R.drawable.ic_launcher);
/**
* 1.按钮中显示的文本内容 2点击按钮时触发的事件
* 在Android2.3.1平台下三个按钮的位置是固定的 分别在最左侧 右侧 居中
* 在4.0平台下 setPositiveButton和setNegativeButton位置恰恰相反 分别置于最右侧和最左侧
*/
alertDialog2.setMessage("亲,确定要退出吗?");
alertDialog2.setPositiveButton("确定", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, ""+which, Toast.LENGTH_SHORT).show(); }
});
alertDialog2.setNegativeButton("取消", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, ""+which, Toast.LENGTH_SHORT).show(); }
}); alertDialog2.setNeutralButton("再看看", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, ""+which, Toast.LENGTH_SHORT).show(); }
});
alertDialog2.create();
alertDialog2.show();
break;
//显示普通item列表的Dialog
AlertDialog.Builder alertDialg3 = new AlertDialog.Builder(MainActivity.this);
alertDialg3.setTitle("提示");
alertDialg3.setIcon(R.drawable.a);
final String[] colors = getResources().getStringArray(R.array.arrColors);
alertDialg3.setItems(colors, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {// int which 被点击item的下标
Toast.makeText(MainActivity.this, colors[which],Toast.LENGTH_SHORT).show(); }
}); alertDialg3.create();
alertDialg3.show();
//显示Adapter适配器列表的Dialog
list.clear();
AlertDialog.Builder dialog4 = new AlertDialog.Builder(MainActivity.this);
dialog4.setTitle("提示");
dialog4.setIcon(R.drawable.a);
int[] images = {R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4,R.drawable.pic5};
for (int i = 0; i < images.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("Image", images[i]);
map.put("Content", "item"+(i+1)); list.add(map);
} SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, list, R.layout.item_dialog, new String[]{"Image","Content"}, new int[]{R.id.iamgeView,R.id.textView});
dialog4.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, (String)list.get(which).get("Content"), Toast.LENGTH_SHORT).show(); }
});
dialog4.create();
dialog4.show();
//显示多选列表的Dialog
AlertDialog.Builder dialog6 = new AlertDialog.Builder(MainActivity.this);
dialog6.setTitle("选择颜色");
dialog6.setIcon(R.drawable.a);
dialog6.setMultiChoiceItems(R.array.arrColors, new boolean[]{true,true,false}, new OnMultiChoiceClickListener() { @Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) { }
});
dialog6.setPositiveButton("确定", new OnClickListener() { @Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "选择完成", Toast.LENGTH_SHORT).show(); }
}); dialog6.create().show();
//显示日期选择的Dialog
/**
* 弹出日期选择对话框
* 1.上下文对象
* 2.设置日期时回调的方法
* 3.默认的年份
* 4.默认的月份
* 5.默认的日期
*/
DatePickerDialog datePickerDialog = new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() { @Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(MainActivity.this, year+"年"+(monthOfYear+1)+"月"+dayOfMonth+"日", Toast.LENGTH_SHORT).show(); }
}, 2016, 5, 9); datePickerDialog.show();
// 显示日期选择的Dialog
/**
* 3.默认的小时
* 4.默认的分钟
* 5.是否已24小时计时法计时 true代表24h false代表12h
*/
TimePickerDialog timePickerDialog = new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() { @Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
Toast.makeText(MainActivity.this, hourOfDay+"时"+minute+"分", Toast.LENGTH_SHORT).show(); }
}, 14, 54, true);
timePickerDialog.show();
AlertDialog.Builder builder = new AlertDialog.Builder(MainDialog.this);
LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.test, null);
builder.setIcon(R.drawable.icon);
builder.setTitle("自定义输入框");
builder.setView(textEntryView);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { EditText userName = (EditText) textEntryView.findViewById(R.id.etUserName);
EditText password = (EditText) textEntryView.findViewById(R.id.etPassWord);
showDialog("姓名 :" + userName.getText().toString() + "密码:" + password.getText().toString() );
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) { }
});
builder.create().show();
消除对话框
dismissDialog(int id)会保留这个对象
removeDialog(int id)会删除这个对象和清除状态
另外如果你在弹出窗口的时候希望返回键失效的话,则可以使用Builder.setCancelable(false),那么返回键就会不起作用了
cancel和dismiss方法的本质是一样的,都是从屏幕中删除Dialog,唯一的区别是调用cancel会调用DialogInterface.OnCancelListener.
Android 基础UI组件(二)的更多相关文章
- Android 基础UI组件(一)
1.Toast //显示文字 Toast.makeText(this,"Toast显示文本",Toast.LENGTH_SHORT).show(); //显示图片 Toast to ...
- Android常见UI组件之ListView(二)——定制ListView
Android常见UI组件之ListView(二)--定制ListView 这一篇接上篇.展示ListView中选择多个项及实现筛选功能~ 1.在位于res/values目录下的strings.xml ...
- Android基础测试题(二)
今天给大家带来的是Android基础测试题(二) 题目要求: 定义一个5位长度的整型数组并初始化,然后构建方法根据用户传入的数字判断是否存在数组中,如果存在,返回所在位置,如果不存在,返回-1 首先第 ...
- Android之UI编程(二):表格布局
表格布局(TableLayout)继承了LinearLayout,它的本质依然是线性布局管理器,表TableLayout采用行.列的形式来管理UI组件,它并不需要明确地声明暴行多少行.多少列,而是通过 ...
- Android 高级UI组件(二)
1.ExpandableListView 显示垂直滚动两级列表的条目,只允许两个层次 整体思路: 要给ExpandableListView设置适配器,那么必须先设置数据源. 数据源,就是此处的适配器类 ...
- Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性
1.XML文件中的OnClick 属性可以指定在Activity中处理点击事件的方法,Activity中必须定义该属性指定的值作为方法的名字且有一个View类型的参数,表示此物件被点击. 2.使用se ...
- Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewAnimator及其子类
AdapterViewAnimator:当在视图间切换时会显示动画. android:animateFirstView 定义ViewAnimation首次显示时是否对当前视图应用动画. android ...
- Android 高级UI组件(三)
一.popupWindow 1.AlertDialog和PopupWindow最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManage ...
- Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解
ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...
随机推荐
- 【接口】HttpClient 处理get和post请求(二)(2019-07-14 18:41)
一.环境准备 1.导入httpClient依赖包 <dependency> <groupId>org.apache.httpcomponents</groupId> ...
- 单线程 Redis 为什么这么快,看看这篇就知道了
Redis 作为一种 KV 缓存服务器,有着极高的性能,相对于 Memcache,Redis 支持更多种数据类型,因此在业界应用广泛. 记得刚毕业那会参加面试,面试官会问我 Redis 为什么快,由于 ...
- <C#任务导引教程>练习三
/*Convert.ToInt("213165");int a=12345;string sn=a.ToString();//把a转换成字符串snint b=int.Parse(s ...
- 快速从零开始整合SSM,小白包会(1)
整合SSM,关键就是几个xml的配置. 准备: 1. Idea(配置好tomcat,可以安装插件freeMybatis,提高效率,安装插件不难,百度经验就有) 2. 下载好数据库MySql,以 ...
- [atAGC013F]Two Faced Cards
先对$c_{i}$离散到$[0,n]$上,并令$a_{i},b_{i},d_{i},e_{i}$对应到第一个大于等于他的数 考虑若$a_{n+1}$和$b_{n+1}$也已经确定如何做: 有一个$o( ...
- 快上车丨直播课“Hello ArkansasUI:初识Slider组件(eTS语言)”来啦!
11月24日19:00-20:30,Hello HarmonyOS系列课程第二期线上直播,将手把手教你使用最新的ArkUI进行开发,学习eTS语言.Slider组件和Image组件.完成本期直播课的学 ...
- 【JavaSE】IO(1)-- File类
File类 2019-07-01 22:41:42 by冲冲 在 Java 中,File 类是 java.io 包中唯一映射磁盘文件本身的对象.File类可以获取文件的相关信息(查看文件名.路径. ...
- 设计模式学习-使用go实现访问者模式
访问者模式 定义 优点 缺点 适用范围 代码实现 什么是 Double Dispatch 参考 访问者模式 定义 访问者模式(Visitor):表示一个作用于某对象结构中的各元素的操作.它使你可以在不 ...
- AGC050B Three Coins
做的时候有思考到是否能转化成移动点问题,但是没有清晰的把他解释出来. NOIP的时候也一样,T3也有考虑到是否能转为差分,但是也没有清晰的写出来. 自己做题的时候应尽量保证草稿纸和思绪的清晰,而不是在 ...
- 【 [SCOI2016]幸运数字】
P3292 [SCOI2016]幸运数字 想法 倍增加上线性基就行惹 线性基的合并可以通过把一个线性基的元素插入到另一个里实现 #include<iostream> #include< ...