android常用控件总结(一)

android 常用控件
1.ImageView
2.EditText
3.Button
4.RadioButton
5.CheckBox


1.ImageView
属性: src 设置图片
layout_width宽
layout_height高

android:maxHeight="100dp" //最大高度
android:maxWidth="100dp" //最大宽度
adjustViewBounds自动缩放按照宽或高
这;两个要结合android:adjustViewBounds="true"属性使用,否则无效

android:scaleType 设置图片的显示类型(8种类型)

1."matrix"默认的 :不改变原图的大小,从ImageView的左上角开始绘制原图,原图超过ImageView的部分做裁剪处理
2."center" 保持原图的大小,显示在ImageView的中心。当原图的size大于ImageView的size,超过部分剪裁处理
3."centerCrop":以整个ImageView为目的,将原图的中心对准ImageView的中心,等比例放大原图,直到原图填充满整个ImageView为止(ImageView的宽高都要填满),原图超过ImageView的部分作裁剪处理
4."centerInside":以原图显示为目的,将图片的内容完整居中显示,通过比例缩小原图的 size宽(高),等于或小于ImageView的宽(高).如果原图的size本身就小于ImageView的seize,则原图不做任何处理,居中显示在ImageView
5."fitCenter":把原图按比例扩大或缩小到ImageView的 高度,居中显示
6."fitStart" :把原图扩大或缩小到ImageView的高度(宽度),显示在ImageView的上部分
7."fitEnd":将原图扩大或缩小到ImageView的 高度(宽度),显示在ImageView的下部分
8."fitXY":把图片按照指定的ImageView的大小拉伸显示,不保持原比例,填满ImageView


android:cropToPadding 是否截取指定区域用空白代替。
单独设置无效果,需要与scrollY="10dp"或android:scrollX="10dp"一起使用


2.EditText
I.属性:
android:hint 设置提示的文字
android:background 设置背景

android:imeOptinos可对Android自带的软键盘进行一些界面上的设置
android:imeOptions="flagNoExtractUi" //使软键盘不全屏显示,只占用一部分屏幕,同时,这个属性还能控件软键盘右下角按键的显示内容,默认情况下为回车键
android:imeOptions="actionNone" //输入框右侧不带任何提示
android:imeOptions="actionGo" //右下角按键内容为'开始'
android:imeOptions="actionSearch" //右下角按键为放大镜图片,搜索
android:imeOptions="actionSend" //右下角按键内容为'发送'
android:imeOptions="actionNext" //右下角按键内容为'下一步'
android:imeOptions="actionDone" //右下角按键内容为'完成'
注意:如果无效,则需和android:inputType="text"或者android:singleLine="true" 属性配合使用


II.捕捉用户点击键盘右下角的按钮的监听事件editText.setOnEditorActionListener
在xml中设置id属性:android:id="@+id/et"
// 找到EditText
EditText editText = (EditText) findViewById(R.id.et);
//设置监听事件
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
//Log.e("bm", "我点击了键盘有下角的按钮");
Toast.makeText(MainActivity.this, "text2", Toast.LENGTH_SHORT).show();
return false;
}
});
III.获得文本getText()
注意:返回值:Editable,所以使用该方法时需要加toString()
String text = et.getText().toString();


3.Button
Button 添加图片->文字图片按钮
android:drawableLeft="@drawable/xxx"
ImageButton
没有文字只有图片 设置图片用src
使用:1.在drawable里面导入一张图片
2.给imageButton设置图片
xml→android:src="@drawable/xxx"
setImageResource()

Button的设置点击事件的三中方法
1.在xml中添加onClick属性
1.1android:onClick="xxx"
1.2使用:在activity里增加 该方法
注意:xml里面onClick的属性值即为activity里增加的方法名
public void 参数view对象

2.类或则匿名内部类实现OnClickListener(处理一个Button,多个繁琐)
2.1找到Button-->findViewById()
2.2设置点击(监听)事件
3.主类继承OnClickListener,重写onClick方法(使用switch多分支处理多个button)
3.1找到Button
3.2设置点击(监听)事件 btn.setOnClickListener(this);
3.3让主类实现 View.OnClickListener接口
3.4重写onClick方法->使用是switch(v.getId())来获取对应的id设置对应的点击事件

4.RadioButton(单选按钮)及RadioGroup的用法【重点】
4.1 RadioButton、RadioGroup的使用
!!去掉小圆圈 button="@null"!!

<RadioGroup 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" >

<RadioButton
android:id="@+id/rbtn01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClick"
android:text="男" />

<RadioButton
android:id="@+id/rbtn02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClick"
android:text="女" />

<RadioButton
android:id="@+id/rbtn03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRadioButtonClick"
android:text="保密" />

</RadioGroup>
设置点监听事件(过于繁琐)
public void onRadioButtonClick(View v)v{
switch(v.getId()){
//根据ID来获取对应的点击事件
}
}


4.2 绑定RadioGroup特有监听器
4.2.1在xml中给RadioGroup一个id值 -> android:id="@+id/radioGroup"

4.2.2根据id找到RadioGroup实例 RadioGroup group = (RadioGroup)findViewById(R.id.radioGroup);

4.2.3绑定一个匿名监听器 setOnCheckedChangeListener
group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 根据ID获取RadioButton的实例
RadioButton rb = (RadioButton) findViewById(checkedId);
Toast.makeText(MainActivity.this, rb.getText().toString(),
Toast.LENGTH_SHORT).show();
}
});
5.CheckBox(复选按钮)
1 CheckBox的使用:
在xml中声明CheckBox
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.example.day003_checkbox.MainActivity" >

<CheckBox
android:id="@+id/cb1"
android:layout_width="wrap_content"
android:οnclick="onCheckBoxClick"
android:layout_height="wrap_content"
android:text="辣条" />
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:οnclick="onCheckBoxClick"
android:text="麻辣烫" />
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:οnclick="onCheckBoxClick"
android:text="小龙虾" />
</LinearLayout>

响应点击事件
public void onCheckBoxClick(View view) {
boolean isCheck = ((CheckBox) view).isChecked();
switch (view.getId()) {
case R.id.ck1:
if (isCheck) {
Toast.makeText(MainActivity.this, "我想吃辣条!", 0).show();
}else{
Toast.makeText(MainActivity.this, "不吃不吃!", 0).show();
}
break;
case R.id.ck2:
if (isCheck) {
//选中该项
}else{
//取消选中该项
}
break;
case R.id.ck3:
if (isCheck) {
}else{
}
break;
}
}

2 绑定CheckBox特有监听器
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
Toast.makeText(MainActivity.this, "111", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this, "222", Toast.LENGTH_SHORT).show();
}
}
});
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值