android – 如何做一个findViewById(R.id.>> StringVarHere <<)?

前端之家收集整理的这篇文章主要介绍了android – 如何做一个findViewById(R.id.>> StringVarHere <<)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
搜索和工作了很长一段时间 – 没有运气. (一定很简单?谢谢你的帮助.)

试图获取/设置一个充满EditTexts文本的屏幕,但不是采用通常的,更加硬编码的方式:

... findViewById (R.id.SomeTextWidgetId) ;

相反,我试图通过一个包含(String)name_of_widget的变量来找出一种可重用的方法.

在伪代码中:

findViewById(R.id.>> StringVarHere<<); // 怎么做 ? 我也尝试了这个findViewById方法,但它不起作用(!?)

//// given:

static final String FIELD_TV_FEE   = "TextViewFee" ;
static final String FIELD_TV_FOO   = "TextViewFoo" ;
static final String FIELD_TV_FUM   = "TextViewFum" ;
//// and some arbitrary number more of similar fields  

static final String [] ALL_FIELDS = {

    FIELD_TV_FEE,FIELD_TV_FOO,FIELD_TV_FUM   // ...
 } ; 

//// ...

    //// this part works
    int ResourceID;
    String stringVarHere = FIELD_TV_FEE; 

    //// outputs a correct id,say '0x7f05000f' as in R.id.xxx below
    ResourceID = context
                       .getResources()
                       .getIdentifier ( stringVarHere,"id",context 
                                          .getApplicationInfo()
                                          .packageName
                                      ) ;
    Log.d ("MyClass","RESID = " + Integer.toHexString(ResourceID) ) ;  
/*
 * that's where I'm stuck ^^^ ... how do I do:
 */

String field_name ;

for ( field_name : ALL_FIELDS ) {
    (EditText) SomethingLike_a_findViewById(field_name).setText ("Hello Wurld") ;
}

我试过.setId ……

//// details

    <!-- excerpt from working xml layout -->
    <EditText
        android:id="@+id/TextViewFee"
        android:inputType="text"
        android:layout ... etc ...         
        />
    <EditText
        android:id="@+id/TextViewFoo"
        android:inputType="text"
        android:layout ... etc ...         
        />
    <EditText
        android:id="@+id/TextViewFum"
        android:inputType="text"
        android:layout ... etc ...         
        />

正如所料,gen’ed R文件有这样的东西:

// ...
public static final class id {
    public static final int TextViewFee=0x7f05000f;
    public static final int TextViewFum=0x7f05001c;
    public static final int TextViewFoo=0x7f05001d;
    // ... etc

是的,谢谢 – 在活动中这样做是有意义的.我试图阻止它变得过于笨重.根据您和A-C的有用建议,这就是我现在正在做的事情.目的是将表单的所有字段文本放回一个String []中. (我知道我也可以对所有领域进行暴力破坏.)

你们下面对此有何看法 – 看起来与你的建议非常相似,madlymad?我想知道这是一个糟糕的设计方法吗?

public class FoodBar {

private Activity activity; 
private Context ctx;

public  FoodBar ( Activity _activity ) {        
    this.activity = _activity;
    this.ctx = this.activity.getApplicationContext() ;
}

public String[] getTextFromAllEditTexts () { // the UI views

    int res_id = 0;
    int i = 0;   

    String [] retValues = new String [MyClassName.ALL_FIELDS_LENGTH] ;

    for (String field : MyClassName.ALL_FIELDS_ALL_VEHICLES) {

       res_id = this.ctx.getResources()
                        .getIdentifier ( field,this.ctx.getPackageName() );

           ((EditText) this.activity
                           .findViewById (res_id))
                           .setText( "Meat and Potatoes" ) ;

               // redundant - get it right back to make sure it really went in !  
        retVal[i++] = ((EditText) this.activity
                                        .findViewById (res_id))
                                        .getText().toString() ;
    }

     return retVal;

} // end func
} // end class

然后从Activity类中,它只是:

String [] theFields = null;
FoodBar = new FoodBar (this);

try {

     theFields = FoodBar.getTextFromAllEditTexts ();

} catch (Exception e) {
     Log.d ("OOPS","There's a big mess in the Foodbar: " + e.toString() );
}

解决方法

你可以这样做的方式是(据我了解你的方式):

这可以在非Activity(YourClassname.java)中:

public static int getMyId(Context context,String field) {
     return context.getResources().getIdentifier (field,context.getPackageName());
}

在Activity-class中:

for ( String field_name : YourClassname.ALL_FIELDS ) {
    int resid = YourClassname.getMyId(context,field_name);
    if(resid != 0) { // 0 = not found 
       EditText et = (EditText) findViewById(resid);
       if (et != null) {
          et .setText ("Hello Wurld") ;
       }
    }
}

但我认为在活动类中编写代码更好:

String packageName = getPackageName();
Resources res = getResources();
for ( String field_name : YourClassname.ALL_FIELDS ) {
    int resid = res.getIdentifier (field_name,packageName);
    if(resid != 0) {// 0 = not found 
       EditText et = (EditText) findViewById(resid);
       if (et != null) {
          et .setText ("Hello Wurld") ;
       }
    }
}

猜你在找的Android相关文章