String data="tv";
同样在我的 xml 文件中,我有一个名为 tv1 的 TextView。我已经在 Activity 中投射了 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>
使用的图像...