我在我的MainLayout.xml
中有更多的100 button
,我想處理每個點擊。我想分組所有的按鈕,然後聽取點擊組按鈕,獲取ID
並將其放入String
,然後將其用作我的基本ID從我的Sqlite Databse Table
查詢。我怎樣才能做到這一點?到目前爲止,我在我的MainClass.java
上有這個。Android識別多個按鈕
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
}
}
正如你可以看到,如果我繼續這樣做,我會很早就代碼,我想縮短我的代碼,而不是上面的代碼,我想分組按鈕,然後聽每個點擊就像RadioButton
,但注意。我不想使用RadioButton。我試圖實現這個seudo代碼。
// 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
任何幫助,不勝感激!非常感謝你!!!