Android常用控件
首先我们可以分为几类常用控件:
文本类控件:
TextView 负责展示文本,非编辑
EditText 可编辑文本控件
按钮类控件:
Button 按钮
RadioButton与RadioGroup 单选按钮
CheckBox 复选按钮
图片控件:
ImageView 负责显示图片
文本类控件TestView
<TextView
//控件id
android:id = "@+id/xxx" @+id/xxx为你定义的该控件的id
//宽度与高度
android:layout_width="match_content" //match_parent表示让当前控件的大小和父布局的大小一样,也就是由父布局来决定当前控件的大小
android:layout_height="wrap_content" //wrap_content表示让当前控件的大小能够刚好包含住里面的内容,也就是由控件内容决定当前控件的大小
//文本文字
android:text="@string/hello_world" //两种方式,直接具体文本或者引用values下面的string.xml里面的元素
//字体大小
android:textSize="24sp" //以sp为单位
//字体颜色
android:textColor="#0000FF" //RGB颜色
//字体格式
android:textStyle="normal" //normal,bold,italic分别为正常,加粗以及斜体,默认为normal
//文本显示位置
android:gravity="center" //来指定文字的对齐方式,可选值有 top、bottom、left、right、center 等
文本类控件EditText
大部分的控件与TextView相同,稍有多出以下一些
//输入内容设置为password类型
android:password="true" //输入的内容会变成*******
//文本提示内容
android:hint="hello_world" //android:hint只是提示作用,真正需要输入的时候提示的内容会消失
按钮类控件Button
在代码块中的一些必须有的条件与上述的大致相同,稍有不同的是需要赋一个ID,并且在Activity中要对其添加一个监听事件,说到监听事件,其有两种方法,两种方法分别适用于两种不同情况,代码如下:
button = (Button) findViewById(R.id.button);
//为button按钮注册监听器,并通过匿名内部类实现
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//点击Button会改变edittext的文字为"点击了Button"
edittext.setText("点击了Button");
}
});
首先这种方法适用于对于一个按钮控件的时候,它对单独的按钮控件添加一个监听事件
button.setOnClickListener(this);
button2.setOnClickListener(this);
@Override
//用switch区分是哪个id
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
edittext.setText("点击了Button");
break;
case R.id.button2:
edittext.setText("点击了Button2");
break;
}
}
而这种方法则是通过switch区分其id,然后分别添加监听事件,如果是多个按钮控件是就可以用到这种方法了
按钮类控件RadioButton、RadioGroup
RadioButton又称其为单选按钮,换句话来说也就是我们平常一些登录页面中的勾选记住密码的那个框,它能够点击勾选和不勾选,更方便于用户们进行选择;
RadioGroup则为单选组合框,它的作用是将一个或者几个RadioButton收纳起来,可以形成一种布局,其也要用到setOnCheckedChangeListener来对其进行监听;
下面我们就通过代码来让大家更清楚的认识到这两个控件:
<RadioGroup
android:id="@+id/radio_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//设置RadioButton的排列方式,分为水平排列horizontal与垂直排列vertical
android:orientation="horizontal" >
<RadioButton
android:id="@+id/rd1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//设置单选后紧跟的文本提示文字
android:text="北京"
//设置文字的大小
android:textSize="30sp"
//设置文字的颜色
android:textColor="#0000FF"
//字体格式
android:textStyle="normal" //normal,bold,italic分别为正常,加粗以及斜体,默认为normal
/>
<RadioButton
android:id="@+id/rd2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="上海" />
</RadioGroup>
这是在activity的资源文件中的代码,很明显,它通过用到上面所提到的两个按钮控件来选择一个你的所在地,这也能够让我们很直观的看出来其按钮的作用,而下面的代码则是Activity中的加监听器的部分:
//通过控件的ID来得到代表控件的对象
radiogroup = (RadioGroup) findViewById(R.id.radio_group);
radiobutton1 = (RadioButton) findViewById(R.id.rd1);
radiobutton2 = (RadioButton) findViewById(R.id.rd2);
//调用setOnCheckedChangeListener来对RadioGroup进行监听的代码
radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == radiobutton1.getId()){
textView.setText("北京");
}else if(checkedId == radiobutton2.getId()){
textView.setText("上海");
}
}
});
按钮类控件CheckBox
上面给大家介绍了单选框按钮,那就不得不说一下复选框按钮了,顾名思义,肯定是能够多选的按钮了,没错,它的作用也确实是多选,就比如写个人介绍中的爱好一样,爱好有很多,所以就不得不用到这个CheckBox按钮了;
<CheckBox
android:id="@+id/cb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
//设置复选按钮后紧跟的文本提示文字
android:text="吃"
//设置文字的大小
android:textSize="30sp"
//设置文字的颜色
android:textColor="#0000FF"
//字体格式
android:textStyle="normal" //normal,bold,italic分别为正常,加粗以及斜体,默认为normal/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="唱歌"
android:textSize="30sp"
android:textColor="#0000FF"/>
//为第一个 CheckBox 注册监听
checkbox1.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//如果第一个 CheckBox 被选中
if(isChecked == true){
textView.setText("CheckBox选中吃");
}
}
});
//为第二个 CheckBox 注册监听
checkbox2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//如果第二个 CheckBox 被选中
if(isChecked == true){
textView.setText("CheckBox选中唱歌");
}
}
});
通过上述的代码我们不难看出其的一些要注意的地方和其使用方法;
图片控件ImageView
ImageView这个控件的作用是通过src访问图片文件的,一般是取在drawable中的对象
<ImageView
//控件id
android:id = "@+id/xxx" @+id/xxx表示新增控件命名为xxx
//宽度与高度
android:layout_width="wrap_content" //wrap_content或者match_parent
android:layout_height="wrap_content" //wrap_content或者match_parent
//此外,可以具体设置高度和宽度显示的像素,不过这样设置如果图片尺寸大于设置的显示的尺寸,则图片是显示不全的,这是可以配合android:scaleType属性。
android:layout_width="200dp"
android:layout_height="200dp"
//把原图按照指定的大小在View中显示,拉伸显示图片,不保持原比例,填满ImageButton.
android:scaleType="fitXY"
//图片来源,需要将图片复制放到res/drawable文件夹里面,引用的时候不需要写图片的后缀
android:src ="@drawable/beautiful">
这个控件还有一个很方便的地方,它不需要像按钮类控件需要对每个按钮赋一个ID并且加上监听事件;