2
String data="tv";

また、私のxmlファイルにはtv1というTextViewがあります。アクティビティにtextViewをキャストしました。

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

文字列をTextViewにキャストしたい...

tv1ではなくstringでこの操作を実行できるようにします。

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

これを達成できるかどうか..textViewにIDを動的に割り当てる方法もあります。ありがとう...

これは私のアクティビティコードです:

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

4

5 に答える 5

6

Stringaをaに「キャスト」することはできないTextViewので、実際に尋ねるつもりだったのは、View(整数IDではなく)名前に基づいてに基づいてを見つける方法だと思います。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);
        }
    }
}

R.id.*3行目の「id」は、定数の名前を見ていることを意味します。同じ手法を使用して、名前で他のタイプのリソースを検索できます(たとえば、Drawables)。その場合、「id」を「drawable」に置き換えて、R.drawable.*リソースの1つの名前を指定します。


最後に、Androidドキュメントの警告に注意してください。

注:この関数の使用はお勧めしません。名前よりも識別子でリソースを取得する方がはるかに効率的です。

上記のコードは、との両方 getIdentifier()を呼び出しているため、まったく効率的ではありませんfindViewById()。どちらも比較的安価な操作です。より良い解決策については、私のフォローアップの回答を参照してください。

于 2012-08-30T17:01:01.023 に答える
3

いったいなぜTextViewを文字列に割り当てたいのでしょうか。findTextViewById()キャストの全体的なポイントは、ポリモーフィズムを活用できるようにすることです(つまり、親[View]を子[TextView、Buttonなど]にキャストする)ので、、などを書く必要はありませんfindButtonById()

文字列に対して.setData()を実行することは意味がありませんが、本当に必要な場合は、文字列をサブクラス化してそのメソッドを追加し、その中の文字列の内部値を更新できます。

そして、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(...);
}
于 2012-08-30T17:06:31.213 に答える
3

最初の答えはまだ元の質問に答えているので残しておきますが、言い換えると、それが問題を解決するための最良の方法ではないと思います。

更新された質問に基づいて、実際に行うべきことは、TextViewsを配列または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);
于 2012-08-30T17:12:23.787 に答える
0

たぶん、このアプローチであなたのために働くことができます。これは、TextViewで文字列テキストをプログラムで表現する方法です。

 mTextViewOutput.append(outputString);
于 2014-06-23T16:10:06.533 に答える
0

TextViewクラスにはsetText()メソッドがあります。あなたがする必要があるのはそれをこのように呼ぶことです:

mMyTextViewObject.setText("some text");
于 2018-08-29T19:20:09.153 に答える