将字符串转换为 TextView [英] Cast String to TextView

查看:12
本文介绍了将字符串转换为 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String data="tv";

另外,在我的 xml 文件中,我有一个名为 tv1 的 TextView.我已经在 Activity 中投射了 textView.

TextView tv1 = (TextView) findViewById(R.id.tv1);

<块引用>

我想将字符串转换成 TextView...

这样我就可以对字符串执行此操作,而不是在 tv1 上.

data.setText("abc");//在字符串上..

我能不能做到这一点..还有如何动态地将 id 分配给 textView.谢谢...

这是我的活动代码:

public class Winnings extends Activity {TextView tv1、tv2、tv3、tv4、tv5、tv6、tv7、tv8、tv9、tv10、tv11、tv12、电视13、电视14、电视15;/** 在第一次创建活动时调用.*/@覆盖公共无效 onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.winnings);设置变量();//TODO 自动生成的方法存根backGroundToChange(7);}私人无效设置变量(){//TODO 自动生成的方法存根tv1 = (TextView) findViewById(R.id.TextView01);tv2 = (TextView) findViewById(R.id.TextView02);tv3 = (TextView) findViewById(R.id.TextView03);tv4 = (TextView) findViewById(R.id.TextView04);tv5 = (TextView) findViewById(R.id.TextView05);tv6 = (TextView) findViewById(R.id.TextView06);tv7 = (TextView) findViewById(R.id.TextView07);tv8 = (TextView) findViewById(R.id.TextView08);tv9 = (TextView) findViewById(R.id.TextView09);tv10 = (TextView) findViewById(R.id.TextView10);tv11 = (TextView) findViewById(R.id.TextView11);tv12 = (TextView) findViewById(R.id.TextView12);tv13 = (TextView) findViewById(R.id.TextView13);tv14 = (TextView) findViewById(R.id.TextView14);tv15 = (TextView) findViewById(R.id.TextView15);}无效 backGroundToChange(int 位置){//我想做的是这个..字符串数据=电视"+位置;//将字符串转换为TextView,以便我可以执行此操作...data.setBackgroundResource(R.drawable.option_correct);//所以这样我就不需要实际使用的switch和case了,如下图.....//我们一般是这样实现的/*开关(位置){情况1:tv1.setBackgroundResource(R.drawable.option_correct);休息;案例2:tv2.setBackgroundResource(R.drawable.option_correct);休息;案例3:tv3.setBackgroundResource(R.drawable.option_correct);休息;案例4:tv4.setBackgroundResource(R.drawable.option_correct);休息;案例5:tv5.setBackgroundResource(R.drawable.option_correct);休息;案例6:tv6.setBackgroundResource(R.drawable.option_correct);休息;案例7:tv7.setBackgroundResource(R.drawable.option_correct);休息;案例8:tv8.setBackgroundResource(R.drawable.option_correct);休息;案例9:tv9.setBackgroundResource(R.drawable.option_correct);休息;案例 10:tv10.setBackgroundResource(R.drawable.option_correct);休息;案例 11:tv11.setBackgroundResource(R.drawable.option_correct);休息;案例 12:tv12.setBackgroundResource(R.drawable.option_correct);休息;案例 13:tv13.setBackgroundResource(R.drawable.option_correct);休息;案例 14:tv14.setBackgroundResource(R.drawable.option_correct);休息;案例 15:tv15.setBackgroundResource(R.drawable.option_correct);休息;默认:休息;}*/}

xml 代码....

<?xml version="1.0" encoding="utf-8"?><线性布局 xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="垂直"><滚动视图android:id="@+id/scrollView1"android:layout_width="match_parent"android:layout_height="wrap_content" ><线性布局android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="垂直"><文本视图android:id="@+id/TextView15"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"android:text="5 千万"android:textAppearance="?android:attr/textAppearanceLarge"android:textColor="#D4A017"android:textStyle="粗体"/><文本视图android:id="@+id/TextView14"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"android:text="1 千万"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView13"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"android:text="500 万"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView12"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"android:text="250 万"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView11"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=12,50,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView10"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=6,40,000"android:textAppearance="?android:attr/textAppearanceLarge"android:textColor="#D4A017"android:textStyle="粗体"/><文本视图android:id="@+id/TextView09"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=3,20,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView08"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=1,60,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView07"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=80,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView06"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=40,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView05"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=20,000"android:textAppearance="?android:attr/textAppearanceLarge"android:textColor="#D4A017"android:textStyle="粗体"/><文本视图android:id="@+id/TextView04"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=10,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView03"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=5,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView02"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=2,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/><文本视图android:id="@+id/TextView01"android:layout_width="wrap_content"android:layout_height="wrap_content"android:background="@drawable/option_bar"机器人:重力=中心"安卓:文本=1,000"android:textAppearance="?android:attr/textAppearanceLarge"安卓:textColor="#ffffff"android:textStyle="粗体"/></线性布局></滚动视图>

使用的图片...

解决方案

你不能将 String 转换为 TextView,所以我假设您真正要问的是如何根据名称(而不是整数 ID)找到 View .IOW,您有一个 String,其中包含您的 TextView 的命名标识符.

通常您需要使用 Java 反射来执行此类操作,但 Android 通过使用 getIdentifier().此方法接受一个字符串并返回一个资源 ID (int),允许您按名称使用资源.试试这样的:

private void backGroundToChange(int position) {字符串资源名称=文本视图"+位置;int resourceID = getResources().getIdentifier(resourceName, "id",getPackageName());如果(资源ID!= 0){TextView tv = (TextView) findViewById(resourceID);如果(电视!= null){//在此处对 TextView tv 执行操作...tv.setBackgroundResource(R.drawable.option_correct);}}}

第 3 行中的id"表示您正在查看 R.id.* 常量的名称.您可以使用相同的技术按名称查找其他类型的资源(例如,Drawables).在这种情况下,您可以将id"替换为drawable",并提供您的 R.drawable.* 资源之一的名称.

<小时>

最后,请注意 Android 文档中的警告:

<块引用>

注意:不鼓励使用此函数.按标识符检索资源比按名称检索资源效率更高.

上面的代码根本不是很高效,因为你调用了 both getIdentifier()findViewById(),它们都不是相对便宜的操作.请参阅我的后续回答以获得更好的解决方案.

String data="tv";

Also in my xml file i have a TextView called tv1. I have casted the textView in the Activity.

TextView tv1 = (TextView) findViewById(R.id.tv1);

I want to cast the string into TextView...

so that I can perform this operation on string instead on tv1.

data.setText("abc"); // on the string..

Can i achieve this or not..Also how to dynamically assign an id to a textView. Thanx...

This is my activity code:

public class Winnings extends Activity {

TextView tv1, tv2, tv3, tv4, tv5, tv6, tv7, tv8, tv9, tv10, tv11, tv12,
        tv13, tv14, tv15;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.winnings);
    setupVariables();

    // TODO Auto-generated method stub
    backGroundToChange(7);
}

private void setupVariables() {
    // TODO Auto-generated method stub
    tv1 = (TextView) findViewById(R.id.TextView01);
    tv2 = (TextView) findViewById(R.id.TextView02);
    tv3 = (TextView) findViewById(R.id.TextView03);
    tv4 = (TextView) findViewById(R.id.TextView04);
    tv5 = (TextView) findViewById(R.id.TextView05);

    tv6 = (TextView) findViewById(R.id.TextView06);
    tv7 = (TextView) findViewById(R.id.TextView07);
    tv8 = (TextView) findViewById(R.id.TextView08);
    tv9 = (TextView) findViewById(R.id.TextView09);
    tv10 = (TextView) findViewById(R.id.TextView10);

    tv11 = (TextView) findViewById(R.id.TextView11);
    tv12 = (TextView) findViewById(R.id.TextView12);
    tv13 = (TextView) findViewById(R.id.TextView13);
    tv14 = (TextView) findViewById(R.id.TextView14);
    tv15 = (TextView) findViewById(R.id.TextView15);
}

void backGroundToChange(int position) {

//What i want to do is this..


String data = "tv" + position;

//The casting of string into a TextView and so that I can perform this...

data.setBackgroundResource(R.drawable.option_correct);

// so by this way i don't need the switch and case that was actually used, shown below.....

//We normally implement it this way

    /*
        switch (position) {
    case 1:
        tv1.setBackgroundResource(R.drawable.option_correct);
        break;
    case 2:
        tv2.setBackgroundResource(R.drawable.option_correct);
        break;
    case 3:
        tv3.setBackgroundResource(R.drawable.option_correct);
        break;
    case 4:
        tv4.setBackgroundResource(R.drawable.option_correct);
        break;
    case 5:
        tv5.setBackgroundResource(R.drawable.option_correct);
        break;
    case 6:
        tv6.setBackgroundResource(R.drawable.option_correct);
        break;
    case 7:
        tv7.setBackgroundResource(R.drawable.option_correct);
        break;
    case 8:
        tv8.setBackgroundResource(R.drawable.option_correct);
        break;
    case 9:
        tv9.setBackgroundResource(R.drawable.option_correct);
        break;
    case 10:
        tv10.setBackgroundResource(R.drawable.option_correct);
        break;
    case 11:
        tv11.setBackgroundResource(R.drawable.option_correct);
        break;
    case 12:
        tv12.setBackgroundResource(R.drawable.option_correct);
        break;
    case 13:
        tv13.setBackgroundResource(R.drawable.option_correct);
        break;
    case 14:
        tv14.setBackgroundResource(R.drawable.option_correct);
        break;
    case 15:
        tv15.setBackgroundResource(R.drawable.option_correct);
        break;

    default:
        break;
    }
                   */

}

xml code....

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >



    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >


        <TextView
            android:id="@+id/TextView15"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="5 Crore"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#D4A017"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TextView14"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="1 Crore"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TextView13"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="50 Lakhs"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/TextView12"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="25 Lakhs"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="12,50,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />



        <TextView
            android:id="@+id/TextView10"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="6,40,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#D4A017"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView09"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="3,20,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView08"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="1,60,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView07"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="80,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="40,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />



        <TextView
            android:id="@+id/TextView05"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="20,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#D4A017"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView04"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="10,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="5,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="2,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />


        <TextView
            android:id="@+id/TextView01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/option_bar"
            android:gravity="center"
            android:text="1,000"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#ffffff"
            android:textStyle="bold" />

    </LinearLayout>

</ScrollView>

Images used...

解决方案

You can't "cast" a String to a TextView, so I assume that what you really meant to ask is how to find a View based on its name (not its integer ID). IOW, you have a String that contains the named identifier of your TextView.

Normally you'd need to use Java reflection to do something like this, but Android provides an alternative solution through the use of getIdentifier(). This method takes a string and returns a resource ID (int), allowing you to work with resources by name. Try something like this:

private void backGroundToChange(int position) {
    String resourceName = "TextView" + position;
    int resourceID = getResources().getIdentifier(resourceName, "id",
            getPackageName());
    if (resourceID != 0) {
        TextView tv = (TextView) findViewById(resourceID);
        if (tv != null) {
            // Take action on TextView tv here...
            tv.setBackgroundResource(R.drawable.option_correct);
        }
    }
}

The "id" in line #3 means you're looking at the names of the R.id.* constants. You could use the same technique to find other types of resources by name (for example, Drawables). In that case, you would replace "id" with "drawable" and provide the name of one of your R.drawable.* resources.


Finally, do take note of the warning in the Android documentation:

Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.

The above code is not very efficient at all because you are calling both getIdentifier() and findViewById(), neither of which are relatively cheap operations. See my follow-up answer for a better solution.

这篇关于将字符串转换为 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆