【发布时间】:2015-12-13 13:30:29
【问题描述】:
我的MainLayout.xml 中有多个100 button,我想处理每个点击。我想对所有按钮进行分组,然后听组按钮的单击,获取ID 并将其放入String,然后将其用作我的基本ID,以从我的Sqlite Databse Table 查询。我怎样才能做到这一点?到目前为止,我的 MainClass.java 上有这个。
MainClass.java
public class MainClass extends AppCompatActivity implements View.OnClickListener {
Button B0,B1,B2,B3....;
String baseId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
buttonInitializer();
}
public void buttonInitializer(){
B0 = (Button)findViewById(R.id.B0); B0.setOnClickListener(this);
B1 = (Button)findViewById(R.id.B1); B1.setOnClickListener(this);
...
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.B0:
baseId = "B0";
break;
case R.id.B1:
baseId = "B1";
break;
..........
default:
break;
}
QueryFire(); // Fire a query
}
Public void QueryFire(){
//Fire the query with id of the clicked button
}
}
如您所见,如果我继续这样做,我将有长代码,我想缩短我的代码,而不是上面的代码,我想对按钮进行分组,然后像 @987654328 一样收听每次点击@,但请注意。我不想使用 RadioButton。我正在尝试实现这个伪代码。
// Put the button into RelativeLayout or something and use it to group the button
// OnClick of Each Button inside the GroupButton:
// Get the ID of the Clicked Button:
// Put the ID of Button into String:
// and FIRE my query with ID of Button
任何帮助将不胜感激!非常感谢!!!
【问题讨论】:
标签: java android sqlite android-studio android-button