android的一些基本控件

记录android的一些常用基本控件以及属性。

需要记下的一些属性
属性描述
gravity可以用来指定 view 的内容的对齐方式
textAllCaps在使用 Button 时,text 显示的文本,默认全都会转换为大写,可以通过 textAllCaps 属性来设置是否转换为大写
maxLines指定限制 editText 的最大高度是多少行的高度,可以使用软键盘的回车键
singleLine指定是否限制 editText 单行输入,此时回车键不可用
background在使用 editText 时,需要去掉下划线,可以使用属性值 @null
inputType在使用 editText 时,需要制定输入类型时使用,如输入密码,可以设置为 textPassword,即可不明文显示输入内容

EditText

在使用 editText 输入文本时,可以把软键盘的回车键设置为其他的一些快捷键,如搜索,下一项,发送等,那么如何设置呢?想想,流程应该是这样的。
1.禁掉回车键的使用
2.把回车键设为你需要的效果键
3.添加监听回掉
好的,我们来验证一下吧:
1.如何使回车键不能使用呢,enter 就是换行的作用,我们可以设置行数为一行属性 singleLine 已经被淘汰了,所以自热就使用 maxLines = “1”,但是结果发现还是可以使用回车键,设置 singleLine =”true” 则不可以使用回车键了,但是 ,不明白这是为什么,然后找到了官方文档,是这样说的:

android:maxLinesMakes the TextView be at most this many lines tall
android:singleLineConstrains the text to a single horizontally scrolling line instead of letting it wrap onto multiple lines, and advances focus instead of inserting a newline when you press the enter key.

翻译过来就是(译得不好,见笑了)

maxLines让 TextView 最大的高度为多行的高度
singleLine强制使文本成水平可滚动单行来代替它可以折成多行,当你按下 enter键时,用获取键焦点来代替插入一个新行

就是说当设定 singleLine 才符合我们的要求。

2.好的,我们可以设置 android:imeOptions 属性来设定我们需要的效果键了
actionSearch 对应的就是“搜索”

<EditText
        android:id="@+id/edit_text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:hint="this is EditText"
        android:imeOptions="actionSearch"/>

可以在 as 中查看其他类型
这里写图片描述
3.然后就是为 EditText 设置监听回掉

EditText editText = (EditText) findViewById(R.id.edit_text_view);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                switch (actionId){
                    case EditorInfo.IME_ACTION_SEARCH :
                        Toast.makeText(MainActivity.this, "ACTION_SEARCH", Toast.LENGTH_SHORT).show();
                        break;
                }
                return false;
            }
        });

通过 actionId 和 你在 xml 选择的类型的 id 匹配,就可以知道是否按下了对应的键了,在这做处理即可,可以通过 as 查看对应的类型 id
这里写图片描述

ProgrerssBar 与 SeeBar

两个都是用来显示进度的进度条,使用不外乎就是更新进度,通过拖动进度条获取目标进度等。

// style设定进度条风格
// progress 设定当前进度
// max 设定进度的最大值
<ProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="?android:attr/progressBarStyleHorizontal"
        android:progress="20"
        android:max="100"/>

<SeekBar
        android:id="@+id/see_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="100"
        android:progress="20"/>

效果
这里写图片描述

上面的是 progressBar,下面的 seeBar,可以通过调用 setProgress() 来的设定当前进度,一般就是通过不停的调用 setProgress 方法来实现进度条的效果,此外,我们一般通过拖动进度条来定位到某个进度位置,更常用的是使用 seeBar,progressBar一般就是用单纯的显示进度。
我们可以为 seeBar 设定监听回掉:

SeekBar seekBar = (SeekBar) findViewById(R.id.see_bar);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
               // 进度改变的时候调用,包括通过 setProgress 改变进度和通过拖动进度条改变进度 ,可以通过 getProgress 回去当前进度
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // 开始触摸拖动进度条时调用
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
               // 松开结束拖动时调用
            }
        });

ProgressDialog

ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);   
progressDialog.setTitle("ProgressDialog");
progressDialog.setMessage("loading...");
progressDialog.show();                

在显示 progressDialog 后,我们可以设定取消显示 dialog,通过设定

// 是否可以通过非自己调用dimiss方式取消 dialog
// 即设为 false 时,无论点击dialog以外区域还是按下返回键都不会取消dialog
progressDialog.setCancelable();
// 是否可以通过触摸 dialog 以外的区域来取消 dialog
// 前提是setCancelable为true
progressDialog.setCanceledOnTouchOutside();

需要注意的是,当你可以通过触摸 dialog 以外的区域来取消 dialog 的话,就默认可以通过返回键来取消 dialog,而不管你有没有设定 setCancelable。

对于 progress 两种风格,可以通过 progressDialog.setProgressStyle() 来设定,效果:(通过 setProgress 和 setMax 来控制和更新进度
这里写图片描述 这里写图片描述
也可以为 progressDialog 设置 button,代码和效果:

progressDialog.setButton(DialogInterface.BUTTON_POSITIVE, "dismiss", new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                                progressDialog.dismiss();
                     }
});

这里写图片描述

AlertDialog 、Toast 和 Snackbar

郭大侠的:Android提醒微技巧,你真的了解Dialog、Toast和Snackbar吗?

下一篇小结 android 的几大布局

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值