Android版本:使用findViewById()用字符串/在一个循环

我在做一个Android应用程序,其中有一个数百按钮,每一个特定的回调。现在,我想设置,而不必写几百行代码(用于按钮各一个)这一个循环。 我的问题是:如何才能findViewById不具有静态在键入每个按钮的id? 以下是我想要做的:

 for(int i=0; i<some_value; i++) {
  for(int j=0; j<some_other_value; j++) {
  String buttonID = "btn" + i + "-" + j;
  buttons[i][j] = ((Button) findViewById(R.id.buttonID));
  buttons[i][j].setOnClickListener(this);
  }
 }

在此先感谢!
本文地址 :CodeGo.net/250944/ 

------------------------------------------------------------------------------------------------------------------------- 
1. 您getIdentifier()

for(int i=0; i<some_value; i++) {
 for(int j=0; j<some_other_value; j++) {
 String buttonID = "btn" + i + "-" + j;
 int resID = getResources().getIdentifier(buttonID, "id", "com.sample.project");
 buttons[i][j] = ((Button) findViewById(resID));
 buttons[i][j].setOnClickListener(this);
 }
}


2. 一起来看看这些答案: Android和获取id为铸造一个视图作为字符串 ImageButtons的数组,从一个变量赋值R.view.id 
3. 你可以尝试做一个int []包含所有的按钮的ID,然后遍历是:

int[] buttonIDs = new int[] {R.id.button1ID, R.id.button2ID, R.id.button3ID, ... }
for(int i=0; i<buttonIDs.length; i++) {
 Button b = (Button) findViewById(buttonIDs[i]);
 b.setOnClickListener(this);
}


4. 如果您要访问您可以使用标签。 中的onClick INT I=的Integer.parseInt(v.getTag); 但你不能访问likw这个那个按钮。 简单的编程方式创建按钮 按按钮B=新的Button(本); 
5. 在java代码中的XML来创建自定义按钮,而如下图所示我

Button bs_text[]= new Button[some_value];
 for(int z=0;z<some_value;z++)
  {
   try
   {
   bs_text[z] = (Button) new Button(this);
   }
   catch(ArrayIndexOutOfBoundsException e)
   {
    Log.d("ArrayIndexOutOfBoundsException",e.toString());
   }
  }


6. 如果您的顶层视图只有那些按钮的views,子,你可以做

for (int i = 0 ; i < yourView.getChildCount(); i++) {
 Button b = (Button) yourView.getChildAt(i);
 b.setOnClickListener(xxxx);
}

如果存在多个视图你需要检查是否选择了一个是你的一个按钮。

http://codego.net/250944/

转载于:https://www.cnblogs.com/cmblogs/p/4379119.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是Android Studio失物招领模块Xml文件的实现代码,请修改代码实现下拉以及图片上传功 <TextView android:id="@+id/shiwuzhaolingzi" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="请选择是失物还是招领" /> <Spinner android:id="@+id/spinner_lost_found" android:layout_width="match_parent" android:layout_height="wrap_content" android:entries="@array/lost_found_array" /> <TextView android:id="@+id/text_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品名称" /> <EditText android:id="@+id/edit_item_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="物品描述" /> <EditText android:id="@+id/edit_item_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="时间" /> <EditText android:id="@+id/edit_item_time" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <TextView android:id="@+id/text_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="地点" /> <EditText android:id="@+id/edit_item_location" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" /> <Button android:id="@+id/button_upload_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="上传图片" /> <TextView android:id="@+id/text_image_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="图片路径:" /> <Button android:id="@+id/button_submit" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="提交" /> </LinearLayout>
最新发布
05-17

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值