一、文本框TextView与编辑框EditText
1.TextView文本框。有点类似于Swing的JLabel,只能显示文本,不能编辑
<!-- android:singleLine="true"单行模式,文本框不会换行 -->
<!-- android:ellipsize="middle" 设置中间省略
设置显示的文本超过了TextView的长度时,文本框内默认显示的提示文本 -->
<!--android:autoLink="email"对邮件增加链接
将符合指定格式的文本转换为可单击的超链接形式 -->
<!--android:shadowColor="#0000ff"文本的阴影的颜色
android:shadowDx="15.0"阴影在水平方向上的偏移
android:shadowDy="20.0"阴影在垂直方向上的偏移
android:shadowRadius="45.0"阴影的角度
android:text="@string/tv1"设置文本框的内容
android:textColor="#ff0000"文本框内文本的字体颜色
android:textSize="25pt"文本框内文本的字体大小
android:background="@drawable/ic_launcher"指定背景 带边框的文本
android:drawableLeft="@drawable/ic_launcher"绘制一张图片 带图片的文本-->
2.EditText和TextView有很多相似之处,
它们最大的区别在于TextView不允许用户编辑文本内容,EditText允许用户编辑
二、按钮Button与图片按钮ImageButton
1.Button与ImageButton的区别在于:Button生成的按钮上显示文字,而ImageButton上则显示图片
2.<!-- 普通文字按钮 -->
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red"
android:text="普通按钮"
android:textSize="10pt"/>
<!-- 普通图片按钮 -->
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blue"
android:background="#000000"/>
<!-- 按下时显示不同图片的按钮 -->
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:src="@drawable/button_selector" />
<!-- 带文字的图片按钮 -->
<Button
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button_selector"
android:text="带文字的图片按钮" />
button_selector.xml:
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 指定按钮按钮下时的图片 -->
<item android:state_pressed="true"
android:drawable="@drawable/red"
/>
<!-- 指定按钮松开时的图片 -->
<item android:state_pressed="false"
android:drawable="@drawable/purple"
/>
</selector>
三、单选按钮RadioButton和复选框CheckBox
1.相同:都有一个android:checked属性,用于指定RadioButton、CheckBox初始时是否被选中
2.不同:一组RadioButton只能选中其中一个,
因此RadioButton通常要与RadioGroup一起使用,用于定义一组单选按钮
3.一个实例:
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="11pt"/>
<!-- 定义一组单选按钮 -->
<RadioGroup android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<!-- 定义两个单选按钮 -->
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:checked="true"/>
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"/>
</RadioGroup>
</TableRow>
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色"
android:textSize="11pt"/>
<!-- 定义一个垂直的线性布局 -->
<LinearLayout android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- 定义三个复选框 -->
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:checked="true"/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色"/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色"/>
</LinearLayout>
</TableRow>
四、状态开关按钮ToggleButton
1.它与CheckBox复选框非常相似,主要区别在功能上
<!--android:checked="true"是否被选中
android:textOff="横向排列"
当该按钮没有被选中时显示的文本
android:textOn="纵向排列"
当该按钮被选中时显示的文本 -->
ToggleButton toggle = (ToggleButton) findViewById(R.id.toggle);
final LinearLayout test = (LinearLayout) findViewById(R.id.test);
toggle.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
//设置LinearLayout垂直布局
test.setOrientation(1);
}
else
{
//设置LinearLayout水平布局
test.setOrientation(0);
}
}
});
五、模拟时钟AnalogClock和数字时钟DigitalClock
<!-- 定义模拟时钟 -->
<AnalogClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<!-- 定义数字时钟 -->
<DigitalClock
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14pt"/>
DigitalClock的本质还是一个TextView,有TextView的XML的属性和方法
六、计时器Chronometer
<Chronometer android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15pt"/>
<Button android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始"/>
//获取计时器组件
final Chronometer ch = (Chronometer) findViewById(R.id.test);
//获取开始按钮
Button start = (Button) findViewById(R.id.start);
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//设置开始计时时间
ch.setBase(SystemClock.elapsedRealtime());
//启动计时器
ch.start();
}
});
ch.setOnChronometerTickListener(new OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
// TODO Auto-generated method stub
//如果从开始计时到现在超过了30s
if(SystemClock.elapsedRealtime()-ch.getBase()>30*1000)
{
ch.stop();
}
}
});
七、图像视图ImageView
ImageView继承自View组件,主要功能是用于显示图片--实际上这个说法不太严谨,
因为它能显示的不仅仅是图片,任何Drawable对象都可使用ImageView显示。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="@+id/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn1" />
<Button
android:id="@+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn2" />
<Button
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bn3" />
</LinearLayout>
<!-- 定义显示图片整体的ImageView -->
<ImageView
android:id="@+id/image1"
android:layout_width="fill_parent"
android:layout_height="240pt"
android:background="#0000ff"
android:scaleType="fitCenter"
android:src="@drawable/shuangta" />
<!--
android:scaleType="fitCenter"
设置所显示图片如何缩放或移动以适应ImageView的大小
-->
<!-- 定义显示图片局部细节的ImageView -->
<ImageView
android:id="@+id/image2"
android:layout_width="20dp"
android:layout_height="120dp"
android:layout_marginTop="10dp"
android:background="#0000ff" />
</LinearLayout>
//定义查看下一张图片的监听器
next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(currentImg >= 4)
{
currentImg = -1;
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
//如果图片还未回收,先强制回收该图片
if(!bitmapDrawable.getBitmap().isRecycled())
{
bitmapDrawable.getBitmap().recycle();
}
//改变ImageView显示的图片
image1.setImageBitmap(BitmapFactory.decodeResource(getResources(), images[++currentImg]));
}
});
//定义改变图片透明度的方法
OnClickListener listener = new OnClickListener() {
@Override
public void onClick(View v) {
if(v == plus)
{
alpha += 20;
}
if(v == minus)
{
alpha -=20;
}
if(alpha>=255)
{
alpha = 255;
}
//改变图片的透明度
image1.setAlpha(alpha);
}
};
//为两个按钮添加监听器
plus.setOnClickListener(listener);
minus.setOnClickListener(listener);
image1.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
BitmapDrawable bitmapDrawable = (BitmapDrawable) image1.getDrawable();
//获取第一个图片显示框中的位图
Bitmap bitmap = bitmapDrawable.getBitmap();
//bitmap图片实际大小与第一个ImageView的缩放比例
double scale = bitmap.getWidth()/320.0;
//获取需要显示的图片的开始点
int x =(int) (event.getX()*scale);
int y = (int) (event.getY()*scale);
if(x+120>bitmap.getWidth())
{
x = bitmap.getWidth() - 120;
}
if(y+120 > bitmap.getHeight())
{
y = bitmap.getHeight() - 120;
}
//显示图片的指定区域
image2.setImageBitmap(Bitmap.createBitmap(bitmap,x,y,120,120));
image2.setAlpha(alpha);
return false;
}
});