Android—获取view中的一个控件
2015-08-11 15:44
288 查看
<span style="font-size:14px;"> </span>
<span style="font-size:14px;"><?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="400dip" android:background="#FFFFFF" > <com.insightcode.androidfont.FontTextTextView2 android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="10dip" android:gravity="center" android:text="华南区" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#376092" /> <TextView android:id="@+id/tv_cardview_dailyreport_all_achieve" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/textView1" android:gravity="center" android:paddingLeft="30dip" android:text="1%" android:textAppearance="?android:attr/textAppearanceMedium" android:textColor="#376092" android:textSize="25dip" /> </RelativeLayout></span>
比如main.xml中有两个textview,我们要获取其中一个textview2
其实com.insightcode.androidfont.FontTextTextView2是我自己写一个类,继承TextTiew,为了区别和普通TextView,我设置微软雅黑字体,其实没要求就直接继承作为一个类就可以了
<span style="font-size:14px;">package com.insightcode.androidfont; import com.insightcode.app.BusinessApplication; import android.content.Context; import android.graphics.Typeface; import android.util.AttributeSet; import android.widget.TextView; /** * 文本为汉字,设置微软雅黑,日报的整体跟踪机构名称 * */ public class FontTextTextView2 extends TextView { private Context mContext=null; Typeface texttypeface=null; public FontTextTextView2(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); if(mContext==null){ this.mContext = context; } if(texttypeface==null){ texttypeface = BusinessApplication.texttypeface; } this.setTypeface(texttypeface); } public FontTextTextView2(Context context, AttributeSet attrs) { super(context, attrs); if(mContext==null){ this.mContext = context; } if(texttypeface==null){ texttypeface = BusinessApplication.texttypeface; } this.setTypeface(texttypeface); } public FontTextTextView2(Context context) { super(context); if(mContext==null){ this.mContext = context; } if(texttypeface==null){ texttypeface = BusinessApplication.texttypeface; } this.setTypeface(texttypeface); } }</span>
然后在activity里面写如下函数
<span style="font-size:14px;"> /**
* 从当前view中查找TextView子控件
*
* @param group
* @return
*/
private FontTextTextView2 findTextView2(ViewGroup group) {
if (group != null) {
for (int i = 0, j = group.getChildCount(); i < j; i++) {
View child = group.getChildAt(i);
if (child instanceof TextView) {
return (FontTextTextView2) child;
} else if (child instanceof ViewGroup) {
FontTextTextView2 result = findTextView2((ViewGroup) child);
if (result != null)
return result;
}
}
}
return null;
}</span>
然后使用以上方法:
FontTextTextView2 ftt2 = findTextView2((ViewGroup)view);
ftt2就是要从xml中获取的控件,可以ftt2.getText()获取文本进行操作
本文为原创文章,转载请标明出处!
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories