java Android:按 id 查找按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14286886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: finding button by id
提问by Ziva
I have a problem with finding buttons. I have an AlertDialog
where I choose one of 5 options. When I choose an option I want change the color of the button I clicked. I declared buttons in xml file inside <RealativeLayout>
but when I'm trying to find my button by id (id's are like "id1","id2"...)using the findViewById
method, there is a mistake, which says that I can't use this method like I do:
我在查找按钮时遇到问题。我有一个AlertDialog
我可以选择 5 个选项之一的地方。当我选择一个选项时,我想更改我单击的按钮的颜色。我在里面的 xml 文件中声明了按钮,<RealativeLayout>
但是当我试图通过 id(id 就像“id1”,“id2”......)找到我的按钮时findViewById
,有一个错误,它说我不能像我一样使用这种方法:
AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);
builder.setTitle(R.string.pickColor);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Button btn_tmp;
String theButtonId = "id";
theButtonId = theButtonId+(String.valueOf(which));
btn_tmp = (Button) findViewById(theButtonId);
}
});
How can I fix that or maybe I should use other method?
我该如何解决这个问题,或者我应该使用其他方法?
EDIT:
编辑:
I think I solved my problem. I used one of Button's method: getId() like this:
我想我解决了我的问题。我使用了 Button 的方法之一:getId(),如下所示:
final int id = clickedButton.getId();
final ImageButton btn_tmp;
btn_tmp = (ImageButton)findViewById(id);
回答by Paschalis
Background: IDs are esentially variables
背景:ID 本质上是变量
In the XML
files we give IDs to the resources, and then the compilers use all these to generate the gen/R.java
. So essentially, these IDs are int variables belonging in class R.
An example of R.java:
在XML
文件中,我们为资源提供 ID,然后编译器使用所有这些来生成gen/R.java
. 所以本质上,这些 ID 是属于 R 类的 int 变量。 R.java 的一个例子:
// btw this file should never be edited!
public final class R {
public static final class id {
public static final int id1=0x7f0100aa;
public static final int id2=0x7f0100ab;
}
}
Just because a String
exists that contains a valid name of a variable (R.id.id1
), it can't magically access that variable. To do this, one can use reflection
. However, in this case I believe it is an unnecessary complication, and will even be slower.
仅仅因为 aString
存在包含变量的有效名称 ( R.id.id1
),它不能神奇地访问该变量。为此,可以使用reflection
. 但是,在这种情况下,我认为这是不必要的并发症,甚至会更慢。
findViewById
needs an ID (integer variable):
findViewById
需要一个 ID(整数变量):
You cannot supply a String
to this function. You should use an integer value, and specifically the ones that correspont to int variables in R.java
.
For example:
您不能String
为此函数提供 a 。您应该使用整数值,特别是与R.java
. 例如:
findViewById(R.id.id1) // for your 1st button
Solution:
解决方案:
You can dynamically select the integer value:
您可以动态选择整数值:
AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);
builder.setTitle(R.string.pickColor);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Button btn_tmp;
int chosenID=-1;
switch (which) {
case 1: chosenID=R.id.id1;
break;
case 2: chosenID=R.id.id2;
break;
}
btn_tmp = (Button) findViewById(chosenID);
}
});
It is also suggested to use more explanatory IDs, like: buttonDoThis
, buttonDoThat
.
还建议使用更具解释性的 ID,例如:buttonDoThis
、buttonDoThat
。
回答by Ramesh Sangili
btn_tmp = (Button)findViewById(R.id.YourButtonId);
You are passing string instead of integer.
您正在传递字符串而不是整数。
回答by Code-Apprentice
You are trying to use findViewById() with a String that holds the variable name of the ID. Java doesn't support this kind of dynamic variable names since variable names are only known at compile time, not run time. What are you trying to do? You will need to find another way to solve this problem. Please explain further and we can give suggestions.
您正在尝试将 findViewById() 与包含 ID 变量名称的字符串一起使用。Java 不支持这种动态变量名,因为变量名只能在编译时知道,而不是在运行时知道。你想做什么?您将需要找到另一种方法来解决此问题。请进一步解释,我们可以给出建议。
回答by TouchBoarder
If you are using your own layout.xml for the dialog, you need to inflate it first and give the view to your dialog builder.
如果您在对话框中使用自己的 layout.xml,则需要先对其进行扩充并将视图提供给对话框构建器。
LayoutInflater layoutInflater = (LayoutInflater) StartGameActivity.this.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
final RelativeLayout customDialogView= (RelativeLayout) layoutInflater.inflate(R.layout.custom_dialog_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(StartGameActivity.this);
builder.setTitle(R.string.pickColor);
//give the custom view to your dialog builder.
builder.setView(customDialogView);
builder.setItems(R.array.colorArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Button btn_tmp;
switch(which){
//....cases
case 1:
btn_tmp = (Button) customDialogView.findViewById(R.id.button1);
//set the color of the button selected
break;
case 2:
btn_tmp = (Button) customDialogView.findViewById(R.id.button2);
//set the color of the button selected
break;
}
//....cases
}
});