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
原创粉丝点击