在处理应用程序中的变量类型时,我很头疼。
我尝试在循环中根据我的XML布局中定义的按钮的名称来增加var并将其传递给侦听器。
我想从“ jeton1”开始到“ jeton2”,“ jeton3” ....,但是无法在我的代码中做到这一点(出现错误),var不指向存储在其中的buttons
XML
,并且在调用buttons
侦听器时不显示。
我用已定义的数组进行了测试,但是东西失败了。
以下测试代码仅针对一个button
进行。
帮助将不胜感激。
这是我的代码 XML布局:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="20"
android:columnCount="9">
<Button
android:id="@+id/jeton1"
android:layout_column="0"
android:layout_row="0"
android:text="@string/boutona"
android:layout_height="88dp"
android:layout_width="88dp"
android:textSize="40sp"
android:backgroundTint="#eeceac"
android:textStyle="bold" />
Main Java:
public class MainActivity extends AppCompatActivity {
int i;
String jeton = "";
@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Main loop
for (i = 1; i < 2; i++) {
jeton = "jeton" + i; // Should throw "jeton1", "jeton2".....
final Button jetonnew;
jetonnew = (Button) findViewById(R.id.jeton); // Error 'cannot resolve symbol
// jetonnew = (Button)findViewById(R.id.jeton+i);
// Step 4 Listener
jetonnew.setOnTouchListener(
new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
jetonnew.getBackground().setAlpha(0); // Crash app
jetonnew.setTextColor(Color.parseColor("#2aa17b"));
break;
}
return true;
}
});
}
}
}
非常感谢您的答复和建议。
答案 0 :(得分:1)
如果按钮的数量固定,则可以存储整数数组
int[] ids = {R.id.button1, R.id.button2, ...};
但是,如果要动态添加按钮,则应尝试以编程方式创建按钮
Button newButton = new Button(this);
或者您可以创建一些自定义布局并对其进行充气
inflate(this, R.layout.customLayout, null);
请记住,R.id.someId返回,并且整数不是字符串,因此您无法附加到该字符串。出于同样的原因,在id之后添加字符串也不起作用。
答案 1 :(得分:0)
jetonnew =(Button)findViewById(R.id.jeton + i);
这部分代码不起作用,因为R.id.jeton
是生成的整数而不是字符串。
您应该考虑使用findViewByTag而不是findViewById。您的代码将如下所示:
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="20"
android:columnCount="9">
<Button
android:tag="jeton1"
android:layout_column="0"
android:layout_row="0"
android:text="@string/boutona"
android:layout_height="88dp"
android:layout_width="88dp"
android:textSize="40sp"
android:backgroundTint="#eeceac"
android:textStyle="bold" />
因此在您的Java文件中:
for (i = 1; i < 2; i++) {
jeton = "jeton" + i; // Should throw "jeton1", "jeton2".....
final Button jetonnew;
//findViewByTag here
jetonnew = (Button) findViewByTag(jeton)
另一种方法是仅遍历GridLayout子级,如下所示:
//suppose your gridLayout has id=@+id/gridparent
GridLayout gridParent = (GridLayout)findViewById(R.id.gridparent);
for(int index=0; index<gridParent.getChildCount(); ++index) {
Button nextButton = (Button)gridParent.getChildAt(index);
//attach listener here
}
答案 2 :(得分:0)
如果要使用按钮名称查找按钮的ID:
jetonnew = findViewById(getResources().getIdentifier("jeton" + i, "id", getPackageName()));
答案 3 :(得分:0)
您应该创建网格按钮ID的数组。
@IntegerRes int [] resourceButtonIds = new int[]{R.id.jeton1,R.id.jeton2};
然后相应地修改循环。
for (i = 0; i < resourceButtonIds.length; i++) {
final Button jetonnew;
jetonnew = (Button) findViewById(resourceButtonIds[i]);
// now set all your listeners
}