Android—获取view中的一个控件

通过创建自定义的FontTextTextView2类,继承自TextView,并设置微软雅黑字体。使用findTextView2方法从XML布局中获取该控件,然后可以通过ftt2.getText()来操作文本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<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()获取文本进行操作


本文为原创文章,转载请标明出处!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值