Android 如何获取所有按钮ID';在安卓系统上只需一次
我的Android 如何获取所有按钮ID';在安卓系统上只需一次,android,android-layout,android-xml,Android,Android Layout,Android Xml,我的活动中有16个按钮s,我必须在onCreate()中初始化这些按钮。 有没有办法在一行代码中初始化所有按钮?(循环等) 代码应该从XML布局和流程中获取所有按钮R.id.。假设您将按钮命名为按钮0,按钮1。。按钮15。你可以做: for (int i = 0; i < 16; i++) { int id = getResources().getIdentifier("button_"+i, "id", getPackageName()); button[i] = (Bu
活动中有16个按钮
s,我必须在onCreate()中初始化这些按钮。
有没有办法在一行代码中初始化所有按钮?(循环等)
代码应该从XML
布局和流程中获取所有按钮R.id.
。假设您将按钮命名为按钮0,按钮1。。按钮15
。你可以做:
for (int i = 0; i < 16; i++) {
int id = getResources().getIdentifier("button_"+i, "id", getPackageName());
button[i] = (Button) findViewById(id);
}
for(int i=0;i<16;i++){
intid=getResources().getIdentifier(“button_”+i,“id”,getPackageName());
按钮[i]=(按钮)findViewById(id);
}
好吧,如果所有16个按钮都在一个视图或布局中,那么您可以执行以下操作
ArrayList<View> allButtons;
allButtons = ((LinearLayout) findViewById(R.id.button_container)).getTouchables();
arraylistallbutton;
allButtons=((LinearLayout)findViewById(R.id.button_容器)).getTouchables();
这假设您的容器(在本例中为LinearLayout
)不包含非按钮的可触摸的
使用视图库
下载Android Studio或Intellij IDEA的插件
单击一次即可从当前布局初始化所有视图
对于xamarin android:
List<Button>() allButtons = new List<Button>();
for (int i = 1; i < 15; i++)
{
int id = this.Resources.GetIdentifier("btn" + i.ToString(), "id", this.PackageName);
Button btn = (Button)FindViewById(id);
allButtons.Add(btn);
}
List()所有按钮=新列表();
对于(int i=1;i<15;i++)
{
int id=this.Resources.GetIdentifier(“btn”+i.ToString(),“id”,this.PackageName);
按钮btn=(按钮)FindViewById(id);
所有按钮。添加(btn);
}
此方法使用版面中的所有按钮,希望对您有所帮助。易于实现&几乎每个项目都可以使用它,不需要库
public List<Button> getAllButtons(ViewGroup layout){
List<Button> btn = new ArrayList<>();
for(int i =0; i< layout.getChildCount(); i++){
View v =layout.getChildAt(i);
if(v instanceof Button){
btn.add((Button) v);
}
}
return btn;
}
公共列表getAllButtons(视图组布局){
List btn=new ArrayList();
对于(int i=0;i
示例
List<Button> btn_list = getAllButtons(myRelativeLayout);
Button my_btn = btn_list.get(0);
my_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d("btn_test", "onClick: hello");
}
});
List btn\u List=getAllButtons(myRelativeLayout);
按钮my_btn=btn_list.get(0);
my_btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Log.d(“btn_测试”,“onClick:hello”);
}
});
如果您正在使用Kotlin,并且您的按钮具有button1、button2形式的ID。。。按钮16,您可以执行以下操作:
var btn = ArrayList<Button>(16)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_game_screen)
for(i in 0 until 16) {
var id = resources.getIdentifier("button"+(i+1), "id", packageName)
btn.add(findViewById<View>(id) as Button)
btn[i].setText("something") //or do whatever now
}
}
var btn=ArrayList(16)
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u game\u屏幕)
对于(i在0到16之间){
var id=resources.getIdentifier(“按钮”+(i+1),“id”,packageName)
添加(findViewById(id)作为按钮)
btn[i].setText(“某物”)//或者现在做任何事情
}
}
如果您只知道容器id或按钮id都不同,请尝试以下操作:
活动(在setContentView之前的创建方法上)
List idButtons=new ArrayList();
//容器\按钮是我的按钮容器
RelativeLayout containerButton=findViewById(R.id.container_按钮);
对于(int i=0;i
布局
<RelativeLayout
android:id="@+id/container_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:layout_marginEnd="0dp">
<Button
android:id="@+id/buttonac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red_button"
android:textColor="@android:color/white"
android:text="AC"
android:onClick="pulsacion" />
<Button
android:id="@+id/buttondel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/buttonac"
android:layout_alignBottom="@+id/buttonac"
android:background="@drawable/red_button"
android:textColor="@android:color/white"
android:text="DEL"
android:onClick="pulsacion" />
[...]
</RelativeLayout>
[...]
标识符应该是描述性的,这样代码就不能很好地理解。
<RelativeLayout
android:id="@+id/container_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:layout_marginEnd="0dp">
<Button
android:id="@+id/buttonac"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/red_button"
android:textColor="@android:color/white"
android:text="AC"
android:onClick="pulsacion" />
<Button
android:id="@+id/buttondel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/buttonac"
android:layout_alignBottom="@+id/buttonac"
android:background="@drawable/red_button"
android:textColor="@android:color/white"
android:text="DEL"
android:onClick="pulsacion" />
[...]
</RelativeLayout>