如何做一个findViewById(R.id.&GT;&GT; StringVarHere&LT;&LT;)? [英] How to do a findViewById (R.id.>> StringVarHere << )?
问题描述
搜索并在此工作了半晌 - 没有运气。 (它必须是简单?谢谢你的帮助。)
试图获取/设置一个完整的EditTexts的文字,但没有屏幕与往常一样,更硬codeD的方式:
... findViewById(R.id.SomeTextWidgetId);
相反,我试图找出通过可变控股(字符串)name_of_widget一个可重用的方式。
在伪code:
findViewById(R.id >> StringVarHere&所述。≤); //那怎么办?
我也试过这个<一个href=\"http://stackoverflow.com/questions/6679434/android-findviewbyid-with-a-variant-string\">findViewById方法,但它没有工作(!?)
////给出:静态最后弦乐FIELD_TV_FEE =TextViewFee;
静态最后弦乐FIELD_TV_FOO =TextViewFoo;
静态最后弦乐FIELD_TV_FUM =TextViewFum;
////和一些任意数量更多类似领域的静态最后的String [] = ALL_FIELDS { FIELD_TV_FEE,
FIELD_TV_FOO,
FIELD_TV_FUM // ...
};//// ... ////这部分作品
INT资源ID;
字符串stringVarHere = FIELD_TV_FEE; ////输出正确的ID,说'0x7f05000f如下R.id.xxx
资源ID =上下文
.getResources()
.getIdentifier(stringVarHere,
ID,
背景
.getApplicationInfo()
.packageName
);
Log.d(MyClass的,残差=+ Integer.toHexString(资源ID));
/ *
*这就是我坚持^^^ ...怎么做我做的:
* /串FIELD_NAME;对于(FIELD_NAME:ALL_FIELDS){
(EditText上)SomethingLike_a_findViewById(FIELD_NAME).setText(你好Wurld);
}
我试过.setId ...
////细节 &LT;! - 从使用XML布局摘录 - &GT;
&LT;的EditText
机器人:ID =@ + ID / TextViewFee
安卓的inputType =TEXT
机器人:布局...等等...
/&GT;
&LT;的EditText
机器人:ID =@ + ID / TextViewFoo
安卓的inputType =TEXT
机器人:布局...等等...
/&GT;
&LT;的EditText
机器人:ID =@ + ID / TextViewFum
安卓的inputType =TEXT
机器人:布局...等等...
/&GT;
正如所料,gen'ed R档有这样的事情:
// ...
公共静态最后的类ID {
公共静态最终诠释TextViewFee = 0x7f05000f;
公共静态最终诠释TextViewFum = 0x7f05001c;
公共静态最终诠释TextViewFoo = 0x7f05001d;
// ...等等
是的,谢谢 - 它是有道理这样做的活动。我试图保持它变得太code笨重。下面是我现在在做什么,根据你和A-C的有益的建议。我们的目的是让一种形式回到在一个字符串[]字段中的所有文本。 (我知道我可以蛮力所有字段了。)
你都觉得这个怎么样下文 - 似乎非常相似,你的建议,madlymad?我想知道,如果这是一个贫穷的设计方式?
公共类FoodBar {私人活动活动;
私人上下文CTX;公共FoodBar(活动_activity){
this.activity = _activity;
this.ctx = this.activity.getApplicationContext();
}公众的String [] getTextFromAllEditTexts(){//该UI的意见 INT RES_ID = 0;
INT I = 0; 的String [] = retValues新的String [MyClassName.ALL_FIELDS_LENGTH] 对于(字符串字段:MyClassName.ALL_FIELDS_ALL_VEHICLES){ RES_ID = this.ctx.getResources()
.getIdentifier(字段,ID,this.ctx.getPackageName()); ((的EditText)this.activity
.findViewById(RES_ID))
.setText(香饽饽); //多余的 - 把它右后卫,以确保它真的去了!
retVal的[I +] =((EditText上)this.activity
.findViewById(RES_ID))
.getText()的toString()。
} 返回retVal的;} //结束FUNC
} //结束类
再从Activity类,它只是:
的String [] theFields = NULL;
FoodBar =新FoodBar(本);尝试{ theFields = FoodBar.getTextFromAllEditTexts();}赶上(例外五){
Log.d(OOPS,有一个在Foodbar弄得一塌糊涂:+ e.toString());
}
你可以做的方式是(我了解你们的方式):
这可以在非活动(YourClassname.java):
公共静态INT getMyId(上下文的背景下,字符串字段){
返回context.getResources()则getIdentifier(场,ID,context.getPackageName());
}
在活动类:
为(字符串FIELD_NAME:YourClassname.ALL_FIELDS){
INT渣油= YourClassname.getMyId(背景下,FIELD_NAME);
如果(残油!= 0){// 0 =未找到
等的EditText =(EditText上)findViewById(渣油);
如果(ET!= NULL){
等.setText(你好Wurld);
}
}
}
但我觉得这是更好地code在活动类,如:
字符串的packageName = getPackageName();
资源解析度= getResources();
对于(字符串FIELD_NAME:YourClassname.ALL_FIELDS){
INT渣油= res.getIdentifier(FIELD_NAME,ID,的packageName);
如果(残油!= 0){// 0 =未找到
等的EditText =(EditText上)findViewById(渣油);
如果(ET!= NULL){
等.setText(你好Wurld);
}
}
}
Searched and working on this a long while - no luck. ( It must be simple ? Thanks for the assist. )
Trying to get / set a screen full of EditTexts' text, but not with the usual, more hard-coded way:
... findViewById (R.id.SomeTextWidgetId) ;
Instead, I'm trying to figure out a reusable way via a variable holding the (String) name_of_widget.
In psuedo code:
findViewById (R.id.>> StringVarHere << ); // how to do that ?
I tried also this findViewById method, but it didn't work (!?)
//// 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") ;
}
I've tried .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 ...
/>
As expected, the gen'ed R file has something like this:
// ...
public static final class id {
public static final int TextViewFee=0x7f05000f;
public static final int TextViewFum=0x7f05001c;
public static final int TextViewFoo=0x7f05001d;
// ... etc
Yes, thanks - it makes sense to do it in the activity. I was trying to keep it from getting too code bulky. Here's what I'm doing now, based on your and A-C's helpful suggestions. The intention is to get all the text of fields of a form back in one String[]. (I know I could brute force all the fields too.)
What do you all think about this below - seems very similar to your suggestion, madlymad ? I am wondering if this is a poor design approach ?
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, "id", 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
Then from the Activity class, it's just:
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() );
}
The way you could do it is (as I understand the way you are trying):
This can be in non-Activity (YourClassname.java):
public static int getMyId(Context context, String field) {
return context.getResources().getIdentifier (field, "id", context.getPackageName());
}
in 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") ;
}
}
}
But I think it's better to code in the activity class like:
String packageName = getPackageName();
Resources res = getResources();
for ( String field_name : YourClassname.ALL_FIELDS ) {
int resid = res.getIdentifier (field_name, "id", packageName);
if(resid != 0) {// 0 = not found
EditText et = (EditText) findViewById(resid);
if (et != null) {
et .setText ("Hello Wurld") ;
}
}
}
这篇关于如何做一个findViewById(R.id.&GT;&GT; StringVarHere&LT;&LT;)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!