Android—获取view中的一个控件
来源:互联网 发布:分类信息采集软件 编辑:程序博客网 时间:2024/05/21 09:58
<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()获取文本进行操作
本文为原创文章,转载请标明出处!
0 0
- Android—获取view中的一个控件
- android删除一个view中的子view
- android自定义控件(四) View中的方法
- android自定义控件(四) View中的方法
- android自定义控件(四) View中的方法
- android自定义控件(四) View中的方法
- android自定义控件(四) View中的方法
- Android 获取View在屏幕中的位置
- Android~获取view在屏幕中的位置
- Android获取view在屏幕中的位置
- Android 获取view在屏幕中的位置
- Android 获取view在屏幕中的位置
- Android~获取view在屏幕中的位置
- Android获取View在屏幕中的位置
- Android 获取view在屏幕中的位置
- Android获取view在屏幕中的位置
- android 获取view在屏幕中的位置
- Android 获取view在屏幕中的位置
- ADS1118的MSP430驱动代码,非常方便移植
- 59.strong weak,block,aotoresizing代码实现
- Redis监控方案
- 解决windows gitk显示文件内容中文乱码
- DateTime.Now.ToString() 用法
- Android—获取view中的一个控件
- iOS-开发技巧-UILabel内容模糊的原因
- 史上最详细的Android Studio系列教程三--快捷键
- oc 和swift混编的时候打包ipa安装不了
- the selection cannot be run on any server解决办法
- hdu 1863 畅通工程(kruskal算法)
- HDOJ 1875 畅通工程再续(最小生成树)
- 《Python基础教程(第2版)》学习笔记(二):
- 【Lua】面向对象实现