One in eight billio|

六楼的雨

园龄:12年4个月粉丝:2关注:9

2013-04-06 21:27阅读: 416评论: 0推荐: 0

Android学习总结03之主要控件

Android中的控件非常丰富,Eclipse中打开Android程序的布局文件,以可视化方式编辑布局文件,可看到有很多种的控件,平时主要用到的控件有:TextView、Button、EditText,其他的一些控件如RadioButton、CheckBox、DatePicker、TimePicker、Spinner、AutoCompleteTextView、WebView、TabWidget等等。

各种控件简单记录:

TextView:

在布局文件中,添加TextView控件

在代码中:

TextView tv = new TextView(context);
tv.setText("abc");
setContentView(tv);
1、HTML与TextView:

使用Html.fromHtml()可自定义TextView里显示的文本格式

tv.setText(Html.fromHtml(...<font>...));
2、使用SpannableStringBuilder定义文本格式:
SpannableStringBuilder stylea=new SpannableStringBuilder(astring);
stylea.setSpan(new ForegroundColorSpan(Color.RED),2,22,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(stylea);

Button:

Button事件需要设置监听事件

方法1:实现OnClickListener接口
class ButtonClick implements OnClickListener{
}

btn1.setOnClickListener(new ButtonClick());
方法2:内部匿名类
btn1.setOnClickListener(new OnClickListener(){
}
);
方法3:变量方法,最优
复制代码
//在OnCreate()方法外部声明变量
private OnClickListener listener=new OnClickListener(){
    public void onClick(View v) {
         Button btn=(Button) v;
         switch(btn.getId()){
        }
    }
}

//然后将Button点击事件设置为:
btn1.setOnClickListener(listener);
复制代码

EditText:

EditText是TextView的子类。

在drawable下建立一个xml文件,例如ashape.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <!-- 填充的颜色 -->
    <solid android:color="#FFFFFF" />
    <!-- 设置矩形的四个角为弧形 -->
    <!-- android:radius 弧形的半径 -->
    <corners android:radius="22dip" />
</shape>
复制代码

在Layout中设置为:

android:background="@drawable/ashape"

本文作者:六楼的雨

本文链接:https://www.cnblogs.com/sixlab/archive/2013/04/06/3002987.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

posted @   六楼的雨  阅读(416)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示
评论
收藏
关注
推荐
深色
回顶
收起