2012-08-30 165 views
2
String data="tv"; 

另外在我的xml文件中,我有一个称为tv1的TextView。 我已经铸造了活动中的textView。Cast String to TextView

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

我想串投进去的TextView ...

,这样我可以代替TV1对字符串执行此操作。

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

我可以做到这一点或不..也是如何动态地分配一个id到一个textView。 感谢名单...

这是我的活动代码:

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代码....

<?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> 

图片使用...

option_bar

option_correct

回答

3

你不能“投”一个StringTextView,所以我假设你真正的意思问的是如何根据它的名字(而不是它的整数ID)找到View。 IOW,您有一个String,其中包含您的TextView的指定标识符。

通常情况下,您需要使用Java反射来做类似的事情,但Android通过使用getIdentifier()来提供替代解决方案。此方法接受一个字符串并返回一个资源ID(int),允许您按名称使用资源。尝试是这样的:

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); 
     } 
    } 
} 

“标识”符合#3意味着你在看R.id.*常量的名称。您可以使用相同的技术按名称查找其他类型的资源(例如,Drawable s)。在这种情况下,您会将“id”替换为“drawable”,并提供您的R.drawable.*资源之一的名称。


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

注:使用此功能被劝阻。通过标识符而不是名称检索资源效率更高。

上面的代码是不是很有效,因为在所有您所呼叫getIdentifier()findViewById(),这两者都不是相对便宜的操作。请参阅my follow-up answer以获得更好的解决方案。

+2

+1基于对OP的编辑,这是他正在寻找的答案。谁可以猜到? ;) – Eric

+1

我不知道你是怎么做到的,但是恭喜你解密这个问题。 – kcoppock

+0

我不明白...可以请上传整个解决方案,这将是一个很大的好处.... –

3

为什么你要分配一个TextView到一个字符串?为了您的方便,您可以利用多态性(即将父视图[View]]转换为子视图[TextView,Button等]),以便您不必编写findTextViewById(),findButtonById()等。

这是没有意义的一个字符串运行.setData(),但如果你真的愿意,你也可以继承String和添加方法,并在其更新字符串的内在价值。

而且,在你的XML,你可以说:

<TextView id="@+id/new_id" /> 

这将在运行时生成一个ID为它。

编辑:既然你只是更新一个TextView的背景,和一个TextView只是一个整数,你可以这样做:

public void updateBg(TextView aView) 
{ 
    aView.setBackgroundResource(R.drawable.option_correct); 
} 

// usage, assuming tv1 is already pointing to a TextView 
upodateBg(tv1); 

编辑2:

private ArrayList<TextView> views = new ArrayList<TextView>(); 

//populate 
views.add((TextView)findViewById(R.id.whatever)); 

// 
public void updateView(int index) 
{ 
    (TextView)views.get(index).setBackgroundResource(); 
} 

//usage 
updateView(7); 

编辑3:或者,你可以存储的ID:

private static final int[] tvIds = { R.id.tv1, ... }; 

public void updateBg(int index) 
{ 
    ((TextView)findViewById(tvIds[index])).setBackground(...); 
} 
+0

你想对我更新的问题说点什么 –

+0

@HishamMuneer:更新 – Josh

+0

如果我知道要更新哪个文本视图,我会这样做的。 –

3

我要离开我的第一个答案,因为它仍然回答了原来的问题,但我不认为这是解决问题为你改写它的最佳途径。

根据您的更新问题,你真正应该做的是把你的TextView s转换某种类型的数组或Collection这样你就可以通过位置后访问它们。即使findViewById()是一个相对昂贵的操作,所以你应该尽量减少你的使用。考虑这样的事情:

List<TextView> tv = new ArrayList<TextView>(); 
tv.add((TextView) findViewById(R.id.TextView01)); 
tv.add((TextView) findViewById(R.id.TextView02)); 
tv.add((TextView) findViewById(R.id.TextView03)); 

// This will get TextView02 
// Remember Collections are indexed from zero 
tv.get(1); 
+0

那是一个好办法,但我想尽量减少努力,我已经在我的应用程序中使用了... –

+0

好吧,'集合'方法在短期和长期内确实会最大限度地减少您的工作量。你的'void backGroundToChange(int position)'函数将改变为只用一行来获得适当的'TextView',而不是一长串'switch'或一系列'if'语句...... – user113215

+0

即使没有代码...你说正确使用列表或ArrayList会做到这一点...但我想要更有用的东西... –

0

也许这种方法可以为你工作。它是以编程方式在TextView中表示字符串文本的方式

mTextViewOutput.append(outputString);