我只是想知道,如果有更好的方法可以将setOnClickListener
用于多个按钮并减少冗余代码。
这是我的代码:
private void addListenerOnColorButtons() {
redButton = (Button) findViewById(R.id.buttonRed);
redButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (!red) {
Toast.makeText(Register.this, "Red is selected!",
Toast.LENGTH_SHORT).show();
red = true;
} else {
red = false;
}
}
});
blueButton = (Button) findViewById(R.id.buttonBlue);
blueButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (!blue) {
Toast.makeText(Register.this, "Blue is selected!",
Toast.LENGTH_SHORT).show();
blue = true;
} else {
blue = false;
}
}
});
greenButton = (Button) findViewById(R.id.buttonGreen);
greenButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (!green) {
Toast.makeText(Register.this, "Green is selected!",
Toast.LENGTH_SHORT).show();
green = true;
} else {
green = false;
}
}
});
}
非常感谢任何帮助。
答案 0 :(得分:1)
使您的类实现onClickListener,覆盖类中的OnClickListener()方法。
答案 1 :(得分:1)
你可以这样做:
public class MainActivity extends Activity implements OnClickListener
{
private Button redButton;
private Button greenButton;
private int clickedButtonTag;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.avticity_compte);
redButton = (Button) findViewById(R.id.redButton);
greenButton = (Button) findViewById(R.id.greenButton);
redButton.setOnClickListener(this);
greenButton.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
switch (view.getId())
{
case R.id.redButton:
case R.id.greenButton:
clickedButtonTag = Integer.parseInt((String) view.getTag());
break;
default:
break;
}
}
}
答案 2 :(得分:1)
您只需要实现您的活动
OnClickListener
并使用以下结构调整代码:
private void attachListenersOnColorButtons() {
redButton = (Button) findViewById(R.id.buttonRed);
redButton.setTag("RedButton"); // this tag will be used as identifier in onClick method
redButton.setOnClickListener(this);
blueButton = (Button) findViewById(R.id.buttonBlue);
blueButton.setTag("BlueButton");
blueButton.setOnClickListener(this);
greenButton = (Button) findViewById(R.id.buttonGreen);
greenButton.setTag("GreenButton");
greenButton.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v.getTag().equals("RedButton")){
// actions when red button is pressed
}else if(v.getTag().equals("BlueButton")){
// corresponding actions for blue button
}else if(v.getTag().equals("GreenButton"){
// ...
}
}
答案 3 :(得分:0)
是的,有办法。
在布局文件中设置所有按钮的可点击属性,并在所有按钮中提供相同的方法名称:
<Button
android:id="@+id/buttonBlue"
...
android:clickable="true"
android:onClick="onClick"/>
...
<Button
android:id="@+id/buttonGreen"
android:clickable="true"
android:onClick="onClick"/>
在你的活动中创建一个名为onClick的方法,如下所示:
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonBlue:
// Blue button code
break;
case R.id.buttonGreen:
// Green button code
break;
case R.id.buttonRed:
// Red button code
break;
default:
}
}
答案 4 :(得分:0)
看起来您需要3个切换按钮,然后您不需要布尔值,您的用户将获得一些视觉反馈:http://developer.android.com/guide/topics/ui/controls/togglebutton.html。
避免重复代码的主要方法是将公共代码提取到函数中。确定差异并将它们作为参数传递给这些函数。即使您选择与Button
保持联系,下面也会对此进行演示。
注意由于吐司,我只需要有三个点击的功能,所以如果进行调试,它们都可以是一个:
XML:
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onRedToggleClicked"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onGreenToggleClicked"/>
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onBlueToggleClicked"/>
的活动:
public void onRedToggleClicked(View view) {
colourToggleChanged((ToggleButton) view), "Red");
}
public void onGreenToggleClicked(View view) {
colourToggleChanged((ToggleButton) view), "Blue");
}
public void onRedToggleClicked(View view) {
colourToggleChanged((ToggleButton) view), "Green");
}
private void colourToggleChanged(ToggleButton toggle, String colourName) {
// Is the toggle on?
boolean on = toggle.isChecked();
if (on)
Toast.makeText(this, colourName + " is selected!",
Toast.LENGTH_SHORT).show();
}
答案 5 :(得分:0)
XML
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="18dp"
android:text="red" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button1"
android:layout_below="@+id/button1"
android:text="blue" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/button2"
android:layout_below="@+id/button2"
android:text="green" />
活动
public class MainActivity extends Activity implements OnClickListener{
public Button redButton,blueButton,greenButton;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
redButton=(Button)findViewById(R.id.button1);
blueButton=(Button)findViewById(R.id.button2);
greenButton=(Button)findViewById(R.id.button3);
redButton.setOnClickListener(this);blueButton.setOnClickListener(this);greenButton.setOnClickListener(this);
}
@Override
public void onClick(View arg0)
{
Toast.makeText(MainActivity.this, ((Button)arg0).getText()+" is selected", 0).show();
}
}