铸造字符串的TextView字符串、TextView

2023-09-06 15:41:26 作者:早晚磕废你

 字符串数据=TV; 

此外,在我的xml文件我有一个名为TV1的TextView。我已经铸造TextView的活性的影响。

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

  

我要串投进去的TextView ...

,这样我就可以代替TV1执行上串此操作。

  data.setText(ABC); //串上.. 
安卓设置字符串样式,字符串中部分字体样式

我能实现这个或not..Also如何动态的ID分配给一个TextView。感谢名单...

这是我的活动code:

 公共类奖金延伸活动{TextView的TV1,TV2,TV3,TV4,TV5,TV6,TV7,TV8,TV9,TV10,tv11,TV12,        TV13,TV14,tv15;/ **当第一次创建活动调用。 * /@覆盖公共无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.winnings);    setupVariables();    // TODO自动生成方法存根    backGroundToChange(7);}私人无效setupVariables(){    // 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);//因此通过这种方式,我不需要被实际使用的开关和情况,如下图所示.....//我们通常实行这种方式    / *        开关(位置){    情况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 code ....

 <?XML版本=1.0编码=UTF-8&GT?;< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android机器人:layout_width =match_parent机器人:layout_height =match_parent机器人:方向=垂直><滚动型    机器人:ID =@ + ID / scrollView1    机器人:layout_width =match_parent    机器人:layout_height =WRAP_CONTENT>    <的LinearLayout        机器人:layout_width =match_parent        机器人:layout_height =match_parent        机器人:方向=垂直>        <的TextView            机器人:ID =@ + ID / TextView15            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=5亿卢比            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#D4A017            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView14            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=1亿卢比            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView13            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=50万卢比            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView12            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=25万卢比            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView11            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=12,50,000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView10            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=6,40,000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#D4A017            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView09            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=3,20,000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView08            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=1,60,000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView07            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=80000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView06            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=40000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView05            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=20000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#D4A017            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView04            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=万            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView03            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=5000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView02            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=2000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>        <的TextView            机器人:ID =@ + ID / TextView01            机器人:layout_width =WRAP_CONTENT            机器人:layout_height =WRAP_CONTENT            机器人:背景=@绘制/ option_bar            机器人:比重=中心            机器人:文字=1000            机器人:textAppearance =机器人:ATTR / textAppearanceLarge            机器人:文字颜色=#FFFFFF            机器人:文字样式=大胆/>    < / LinearLayout中>< /滚动型> 

使用的图片...

解决方案

的您不能投在字符串的TextView ,所以我认为你真的是问是如何找到 A 查看基于其名称(而不是它的整数ID)。 IOW,你有一个字符串包含的标识符命名你的的TextView 。的

通常情况下,你可能需要使用Java反射做这样的事情,但Android提供通过使用 则getIdentifier() 。这个方法接受一个字符串,并返回一个资源ID(INT),让您的名字与资源工作。尝试是这样的:

 私人无效backGroundToChange(INT位置){    字符串资源名称=TextView的+位置;    INT RESOURCEID = getResources()。则getIdentifier(资源名称,ID,            getPackageName());    如果(RESOURCEID!= 0){        TextView的电视=(的TextView)findViewById(RESOURCEID);        如果(电视!= NULL){            //就以TextView的电视行动在这里...            tv.setBackgroundResource(R.drawable.option_correct);        }    }} 

标识符合#3意味着你在看 R.id的名字。* 常数。你可以使用同样的技术进行名称查找其他类型的资源(例如,可绘制 S)。在这种情况下,你将取代ID与绘制,并提供你的 R.drawable之一的名称。* 资源。

最后,请注意警告的Andr​​oid文档中:

  

注意:使用此功能气馁这是更有效的来检索标识资源不是名称

以上code是不是很有效,因为在所有您所呼叫的两个 则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.