一步一步学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 。
闂傚倸鍊搁崐鎼佸磹閹间礁纾归柣鎴eГ閸ゅ嫰鏌涢锝嗙8闁逞屽厸閻掞妇鎹㈠┑瀣妞ゆ挾濯Σ鍗炩攽閻愬瓨缍戦柛姘儏宀e灝鈻庨幋婵愭闂佺鍕垫畷闁抽攱鍨堕妵鍕箳閸℃ぞ澹曠紓鍌欑椤︻垶顢氶鐘插灊濠电姵鑹鹃崘鈧銈嗘尵閸犳捇宕㈤幖浣光拺闁硅偐鍋涢崝妤呮煛閸涱喚顬奸柍顏呮尦濮婄粯鎷呴崨闈涚秺椤㈡牠宕卞☉妯碱唶闂佸綊妫跨粈渚€鎮¢垾鎰佺唵閻犺櫣灏ㄩ崝鐔兼煛閸☆厾鐣甸柡灞剧洴婵$兘濮€閳╁啰褰囬梻浣虹帛缁嬪繘宕曞畷鍥潟闁规崘顕х壕鍏肩箾閸℃ê淇柛鐐茬秺濮婃椽鏌呴悙鑼跺濠⒀屽櫍閺屾盯鎮㈤柨瀣淮濡炪們鍨哄Λ鍐€佸鈧幃鈺咁敃椤厼顥氶梺鑽ゅ枑閻熴儳鈧凹鍘剧划鍫ュ礃椤忓棛锛滈柣鐘叉穿鐏忔瑦鏅堕敂绛嬫闁绘劖褰冮弳锝夋煕閳哄绡€鐎规洏鍔戦、娑橆潩椤掑倸顕辨繝纰夌磿閸嬫垿宕愰弽顓炵闁绘劦鍓氶崣蹇涙煣韫囷絽浜炴い鈺傜叀閺屾洝绠涢弴鐐愭盯鏌¢埀顒佺鐎n偆鍘介梺鐟扮摠缁诲啫顔忓┑瀣厱闁圭儤鏌ㄩ。鑲╃磼缂佹ḿ娲寸€殿喖鐖奸獮瀣攽閹邦亞纾惧┑掳鍊楁慨鐑藉磻濞戞碍宕叉慨妞诲亾鐎殿噮鍋婇獮妯肩磼濡粯顏熼梻浣芥硶閸o箓骞忛敓锟�/QQ 1602007闂傚倸鍊搁崐鎼佸磹閹间礁纾归柣鎴eГ閸婂潡鏌ㄩ弮鍫熸殰闁稿鎸剧划顓炩槈濡娅ч梺娲诲幗閻熲晠寮婚悢鍛婄秶濡わ絽鍟宥夋⒑閹肩偛鈧牠宕濋弽顓炍﹂柛鏇ㄥ灠閸愨偓濡炪倖鍔﹀鈧紒顔肩埣濮婂搫煤鐠囨彃绠哄銈冨妼閻楁捇鐛径宀€鐭欐繛鍡樺劤閹垶绻濋姀锝嗙【闁挎洩绠撳畷銏犆洪鍛嫼闂佸憡绻傜€氼剟寮冲▎鎾寸厽婵°倐鍋撴俊顐g懇瀹曟洟顢曢敂瑙f嫽婵炶揪绲块幊鎾活敋濠婂懐纾奸悹鍥ㄥ絻閳ь剙鐏濋悾鐑藉箛閺夊灝绐涙繝鐢靛Т鐎氬嘲煤閹间焦鈷戦悹鍥b偓宕団偓濠氭煃瑜滈崜鐔奉嚕閵婏妇顩烽悗锝庡亞閸樿棄鈹戦埥鍡楃仴婵炲娲滈懞閬嶅锤濡や胶鍘遍棅顐㈡处閹歌锕㈡导瀛樼厪闁搞儜鍐句純濡ょ姷鍋涘ú顓€€侀弮鍫濆耿婵炲棙鍨甸獮鍡涙⒒閸屾瑧顦﹂柟纰卞亰閹本寰勭仦鎯у簥濠电娀娼уΛ娑㈠汲閿曞倹鐓欓柣鎴灻悘宥夋煕椤愵偂閭鐐寸墪鑿愭い鎺嗗亾闁诲浚浜弻锟犲幢濞嗗繋绮堕梺瀹狀潐閸ㄥ潡骞冨▎蹇e晠妞ゆ柨鍚嬮宥呪攽閻樻剚鍟忛柛鐘愁殜楠炴劙骞庨挊澶岀暫閻熸粍妫冮悰顔嘉熼崗鐓庣彴闂佽偐鈷堥崜娑㈠汲娴煎瓨鈷掑ù锝呮啞閸熺偞绻涚拠褏鐣电€规洖缍婇弻鍡楊吋閸涱垽绱遍梻浣告啞濞诧箓宕归柆宥嗗亗闁挎繂顦遍崣鎾绘煕閵夛絽濡介悘蹇庡嵆閹綊骞囬崜浣虹槇閻庢鍣崑濠傜暦婵傜ǹ唯闁挎棁顫夌€氳棄鈹戦悙瀛樺鞍闁糕晛鍟村畷鎴﹀箻鐎靛摜顔曟繛杈剧到閸熷潡宕ュΟ琛℃婵☆垱绮嶅Λ鍐ㄧ暦椤忓懏濯撮悷娆忓閻濇姊婚崒娆戭槮闁规祴鍓濈粭鐔肺旈崨顓犵崶濠殿喗枪缂堜即鍩€椤掆偓閹虫ê顕f繝姘ㄩ柨鏃€鍎抽獮鎰版⒒娴h鍋犻柛搴櫍瀵彃鈽夐姀鐘插殤闂佸搫鍟悧濠囨偂閸愵喗鐓熼柣鏃偳归ˉ瀣极閸儲鈷戦柛婵嗗鐎氭壆绱掓径搴綈濞e洤锕幃婊堟寠婢光斂鍔戦弻鏇熷緞濞戙垺顎嶉梺浼欒缂嶄線寮婚敐澶嬪亜闁告縿鍎抽悡浣糕攽閻橆喖鐏柨姘舵懚閻愮繝绻嗛柕鍫濇噺閸h櫣绱掗悩鍐叉诞婵﹦绮幏鍛村川闂堟稒璐¢柟骞垮灲楠炴帒螖閳ь剟鎮¢弴銏$厱妞ゆ劧绲挎俊鍥煛鐎n偅顥堥柡宀嬬節瀹曟﹢濡搁妷銏犱壕鐟滅増甯掔粣妤呮煙閹殿喖顣奸柛瀣ㄥ妽閵囧嫰寮介悽闈涘煂闂佸搫顑囬崰鏍箖濡も偓椤繈鎮欓鈧锟�
>更多相关文章
首页推荐
佛山市东联科技有限公司一直秉承“一切以用户价值为依归
- 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小时回复排行
热门推荐
最新资讯
操作系统
黑客防御