一步一步学android控件(之二) —— TextView
ndroid 控件众多 , 额 , 具体多少个呢? 貌似有那么几十个吧,也没做个统计,嘿嘿!......
有木有朋友感觉写了那么长时间的android代码,有时候想写点自己的东西的时候却发现自己好像离不开网络耶,什么都需要先到网络上遨游一番才能解决自己的问题。思前想后,个人觉得还是有必要巩固一下自己学习过的东西——想想以前这些东西,自己都写过一遍了,但是折腾一段时间下来都不知道放哪里去了........
好了,废话不多说了,这次准备重新学习一下android的常用控件TextView、EditText、AutoCompleteTextView、Button、CalendarView、CheckBox、Chronometer、CompoundButton、DatePicker、DigitalClock、ExpandableListView、Gallery、GridView、HorizontalScrollView、ImageButton、ImageSwitcher、ImageView、ListPopupWindow、ListView、MultiAutoCompleteTextView、NumberPicker、PopupMenu、PopupWindow、ProgressBar、QuickContactBadge、RadioButton、RadioGroup、RatingBar、RemoteViews、ScrollView、SearchView、SeekBar、SlidingDarwer、Switch、TableHost、TextClock、TextSwitcher、TimePicker、Toast、ToggleButton、VideoView、ViewFlipper、ViewSwitcher、ZoomButton等控件。
今天学习TextView控件,先来看看效果图(注意:本文中的代码是写在工程SelfDefineWidget中的,具体内容参见一步一步学android控件(之一) —— 开始篇
该界面中有5个button和一个TextView控件,当点击每个button时将修改TextView中的内容或背景:
1、点击button “指定字体大小为24” 将看到字体变大,对应属性 android:textSize.
2、点击button “指定字体颜色为蓝色” 字体颜色将从黑色变为男色,对应属性android:textColor.
3、点击button “html下划线” ,将在字体的下面画出一条直线。可以在strings.xml文件中用<u>Some thind </u> 替换硬编码。
4、点击button “html删除线”,将在TextView中的字体的中间画一条直线。可以在strings.xml文件中用<stroke>Some thind </stroke> 替换硬编码。
5、点击button “自定义背景” ,将看到以颜色#e0FFFFCC为底色,#e066CC00为边框色,边框宽度为2的椭圆。
其中1,2,3,4的效果比较简单这里就不给出效果图了,下图为点击“自定义背景”后看到的textView的背景:
从上图可以看到,我们点击了删除线、将字体设置为蓝色,自定义背景的button 。
下面一步一步实现上述所述的功能:
1、首先,打开一步一步学android控件(之一) —— 开始篇 创建的工程,在res/strings.xml文件中加入如下内容:
[html]
<!-- strings for TextView -->
<string name="default_text_view_str">这是默认的TextView的样式</string>
<string name="customer_font_size">指定字体大小为24</string>
<string name="font_size_24">这是24号大小的字体哦</string>
<string name="customer_font_color">指定字体颜色为蓝色</string>
<string name="html_u">html下划线</string>
<string name="html_stroke">html删除线</string>
<string name="cumtomer_bg_str">自定义背景</string>
<!-- end -->
2、在res/values目录下面创建文件widget_color.xml (该文件用来定义所有的颜色),内容如下:
[html]
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="text_view_fill">#e0FFFFCC</color>
<color name="text_view_stroke">#e066CC00</color>
<color name="color_blue">#0000ff</color>
</resources>
3、在drawable(如果不存在,自己新建一个)目录下创建widget_text_view_bg.xml文件(自定义的背景),内容如下
[html]
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@color/text_view_fill" />
<stroke
android:width="2dp"
android:color="@color/text_view_stroke" />
<padding
android:bottom="10dp"
android:left="15dp"
android:right="15dp"
android:top="10dp" />
</shape>
可以看到,定义了一个shape(对应ShapeDrawable类),stroke 描边框 , solid 指定填充颜色,padding 指定内容和边框的距离。
4、创建效果图中的界面text_view_detail.xml
[html]
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<Button
android:id="@+id/customer_font_size_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/customer_font_size" />
<Button
android:id="@+id/customer_font_color_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/customer_font_color" />
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayout1" >
<Button
android:id="@+id/html_u_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/html_u" />
<Button
android:id="@+id/html_stroke_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/html_stroke" />
<Button
android:id="@+id/cumtomer_bg_btn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cumtomer_bg_str" />
</LinearLayout>
<TextView
android:id="@+id/show_text_view_detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="100dp"
android:layout_marginLeft="16dp"
android:singleLine="true"
android:text="@string/default_text_view_str" />
</RelativeLayout>
5、创建用于交互的activity —— WidgetTextView.java
[java]
package com.xy.zt.selfdefinewieget;
import android.app.Activity;
import android.content.res.Resources;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Html;
import android.text.TextPaint;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class WidgetTextView extends Activity implements OnClickListener{
private static final float FONT_SIZE = 24f;
private Button mCusFontSize ;
private Button mCusFontColor ;
private Button mHtmlU ;
private Button mHtmlStroke ;
private Button mCusBg ;
private TextView mShowView ;
private Resources mRes;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text_view_detail);
init();
}
void init(){
mRes = getResources();
mShowView = (TextView) findViewById(R.id.show_text_view_detail);
mCusFontSize = (Button) findViewById(R.id.customer_font_size_btn);
mCusFontSize.setOnClickListener(this);
mCusFontColor = (Button) findViewById(R.id.customer_font_color_btn);
mCusFontColor.setOnClickListener(this);
mHtmlU = (Button) findViewById(R.id.html_u_btn);
mHtmlU.setOnClickListener(this);
mHtmlStroke = (Button) findViewById(R.id.html_stroke_btn);
mHtmlStroke.setOnClickListener(this);
mCusBg= (Button) findViewById(R.id.cumtomer_bg_btn);
mCusBg.setOnClickListener(this);
}
public void onClick(View v) {
String tempStr ;
switch(v.getId()){
case R.id.customer_font_size_btn:
mShowView.setText(R.string.font_size_24);
mShowView.setTextSize(FONT_SIZE);
break;
case R.id.customer_font_color_btn:
mShowView.setTextColor(mRes.getColor(R.color.color_blue));
break;
case R.id.html_u_btn:
tempStr = mShowView.getText().toString();
mShowView.getPaint().setAntiAlias(true);
mShowView.setPaintFlags(Paint.UNDERLINE_TEXT_FLAG);
break;
case R.id.html_stroke_btn:
mShowView.getPaint().setAntiAlias(true);
mShowView.setPaintFlags(Paint.STRIKE_THRU_TEXT_FLAG);
break;
case R.id.cumtomer_bg_btn:
mShowView.setBackgroundResource(R.drawable.widget_text_view_bg);
break;
}
}
}
6、做好这一切后,还需要在一步一步学android控件(之一) —— 开始篇 中WidgetsAdapter类中修改handleItemClicked函数为如下内容:
[java]
Intent intent = new Intent();
switch (action) {
case ViewData.TEXT_VIEW_ID:
intent.setClass(mContext, WidgetTextView.class);
mContext.startActivity(intent);
break;
}
ok,今天就到这里,下一个控件——Button 。
闂傚倸鍊烽懗鍫曘€佹繝鍕濞村吋娼欑壕鍧楁⒑椤掆偓缁夊绮婚弽顓熺厸闁搞儮鏅涜ⅴ闂佺ǹ顑嗛幐鎼佸煡婢跺﹦鏆﹂柛銉㈡櫇閻涖儳绱撻崒娆掝唹闁逞屽墮閸熷潡顢旈鐘亾鐟欏嫭绀冪紒璇茬墦楠炲啴鍩¢崨顖氬絼濡炪倖鎸炬慨铏閵忕姭鏀介柣鎴濇川閸掔増绻涢弶鎴濐伃妤犵偛绻橀弫鎾绘晸閿燂拷/QQ 1602007闂傚倸鍊烽悞锔锯偓绗涘懐鐭欓柟杈鹃檮閸ゆ劖銇勯弽顐粶闁活厽顨堥幉绋款吋婢跺﹦鍘洪梺鍝勫暙閻楀棙鍎梻浣瑰缁诲倿宕愰崨濠勵洸闁诡垎鈧弨浠嬫煟濡法绨块柛蹇撴湰閵囧嫰骞囬鍡欑厯闂佸搫鐭夌换婵嗙暦閹烘垟鍫幖娣灪閺嗙偛鈹戦悙宸殶濠殿喗鎸抽幃褔骞樼拠鑼暰闂佸憡娲﹂崹閬嶅磻閸曨垱鐓i煫鍥ㄦ尰鐠愶繝寮崼銉︹拻濞撴埃鍋撴繛浣冲吘娑樜旈崪浣规櫈闂佸憡渚楅崰姘躲€呴弻銉︾厸闁搞儮鏅涢弸鏃堟煃閻熸壆效闁哄苯绉规俊鐑藉Ψ閵夛附鐦i柣鐔哥矋濠㈡ê螞濡ゅ懏绠掗梻浣呵圭换鎺楀煕閸績鏋旈柡鍐ㄥ€归崰鎰版偨椤栵絽鏋熼悗姘炬嫹
>更多相关文章
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 01-11全球最受赞誉公司揭晓:苹果连续九年第一
- 12-09罗伯特·莫里斯:让黑客真正变黑
- 12-09谁闯入了中国网络?揭秘美国绝密黑客小组TA
- 12-09警示:iOS6 惊现“闪退”BUG
- 03-08消息称微软开发内部AI推理模型,或将成为Op
- 03-08美国法院驳回马斯克请求,未阻止OpenAI转型
- 03-08饿了么成立即时配送算法专家委员会 持续全局
- 03-08长安汽车:预计今年底长安飞行汽车将完成试
- 03-08谷歌推出虚拟试穿、AR美妆新功能
相关文章
24小时热门资讯
24小时回复排行
热门推荐
最新资讯
操作系统
黑客防御