gpt4 book ai didi

android - 如何执行 findViewById (R.id.>> StringVarHere << )?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:55:32 25 4
gpt4 key购买 nike

搜索并为此工作了很长时间 - 运气不佳。 (一定很简单吧?感谢帮助。)

尝试获取/设置一个充满 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 ...
/>

正如预期的那样,生成的 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

是的,谢谢 - 在 Activity 中这样做很有意义。我试图防止它变得太笨重。根据您和 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, "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

然后从 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, "id", context.getPackageName());
}

在 Activity 类中:

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

但我认为在 Activity 类中编写代码会更好:

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

关于android - 如何执行 findViewById (R.id.>> StringVarHere << )?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15179797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com