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组件(二)的更多相关文章

  1. Android 基础UI组件(一)

    1.Toast //显示文字 Toast.makeText(this,"Toast显示文本",Toast.LENGTH_SHORT).show(); //显示图片 Toast to ...

  2. Android常见UI组件之ListView(二)——定制ListView

    Android常见UI组件之ListView(二)--定制ListView 这一篇接上篇.展示ListView中选择多个项及实现筛选功能~ 1.在位于res/values目录下的strings.xml ...

  3. Android基础测试题(二)

    今天给大家带来的是Android基础测试题(二) 题目要求: 定义一个5位长度的整型数组并初始化,然后构建方法根据用户传入的数字判断是否存在数组中,如果存在,返回所在位置,如果不存在,返回-1 首先第 ...

  4. Android之UI编程(二):表格布局

    表格布局(TableLayout)继承了LinearLayout,它的本质依然是线性布局管理器,表TableLayout采用行.列的形式来管理UI组件,它并不需要明确地声明暴行多少行.多少列,而是通过 ...

  5. Android 高级UI组件(二)

    1.ExpandableListView 显示垂直滚动两级列表的条目,只允许两个层次 整体思路: 要给ExpandableListView设置适配器,那么必须先设置数据源. 数据源,就是此处的适配器类 ...

  6. Android用户界面 UI组件--TextView及其子类(二) Button,selector选择器,sharp属性

    1.XML文件中的OnClick 属性可以指定在Activity中处理点击事件的方法,Activity中必须定义该属性指定的值作为方法的名字且有一个View类型的参数,表示此物件被点击. 2.使用se ...

  7. Android用户界面 UI组件--AdapterView及其子类(二) AdapterViewAnimator及其子类

    AdapterViewAnimator:当在视图间切换时会显示动画. android:animateFirstView 定义ViewAnimation首次显示时是否对当前视图应用动画. android ...

  8. Android 高级UI组件(三)

    一.popupWindow 1.AlertDialog和PopupWindow最关键的区别是AlertDialog不能指定显示位置,只能默认显示在屏幕最中间(当然也可以通过设置WindowManage ...

  9. Android用户界面 UI组件--AdapterView及其子类(一) ListView及各种Adapter详解

    ListView就是列表组件,一般通过继承ListActivity使用系统提供的ListView. 所有的AdapterView组件都需要有一个对应的Adapter作为适配器来显示列表中元素的布局方式 ...

随机推荐

  1. supervisor安装

    supervisor管理进程,是通过fork/exec的方式将这些被管理的进程当作supervisor的子进程来启动,所以我们只需要将要管理进程的可执行文件的路径添加到supervisor的配置文件中 ...

  2. java中的泛型设计

    1.为什么要使用泛型程序设计 ArrayList<String> files = new ArrayList<>() 等价于 var files = new ArrayList ...

  3. python微服务

    https://realpython.com/python-microservices-grpc/ https://github.com/saqibbutt/python-flask-microser ...

  4. 连接url

    celery broker redis with password broker_url = 'redis://user:password@redishost:6379/0' tooz zookeep ...

  5. (十)JDBC(重点)

    10.1  数据库驱动 驱动:声卡,显卡,数据库 我们的程序会通过 数据库 驱动和数据库打交道 10.2  JDBC SUN公司为了简化 开发人员的(对数据库的统一)操作,提供了一个(Java操作数据 ...

  6. [第二章]c++学习笔记2(类和对象的基础3)

    隐藏的概念 隐藏的作用 使用例 成员函数的重载与缺省(附使用例) 注意事项

  7. 华为开发者大会主题演讲:3D建模服务让内容高效生产

    内容来源:华为开发者大会2021 HMS Core 6 Graphics技术论坛,主题演讲<3D建模服务使能3D内容高效生产>. 演讲嘉宾:华为消费者云服务 AI算法专家 3D建模服务(3 ...

  8. Python基础(偏函数)

    import functools#functools.partial就是帮助我们创建一个偏函数的,functools.partial的作用就是,把一个函数的某些参数给固定住(也就是设置默认值),返回一 ...

  9. 如何提高C# StringBuilder的性能

    本文探讨使用C# StringBuilder 的最佳实践,用于减少内存分配,提高字符串操作的性能. 在 .NET 中,字符串是不可变的类型.每当你在 .NET 中修改一个字符串对象时,就会在内存中创建 ...

  10. 通过python来获取网页状态

    #!/usr/bin/python import sys,httplibfrom optparse import OptionParserusageString = "Usage: %pro ...