gpt4 book ai didi

java - 安卓按钮 : Shorten OnClick method

转载 作者:行者123 更新时间:2023-11-29 08:44:40 24 4
gpt4 key购买 nike

是否可以让多个按钮调用同一个方法?我的意思是参数将是按钮的 ID。我的问题是我得到了一个非常非常长的 switch case 函数,并且每个 case 都有相同的方法。这是我的代码 fragment (我已将其缩短):

public class HerkunftRind extends Activity implements   View.OnClickListener, Animator.AnimatorListener {

private static final String TAG = "HerkunftRind";
ViewFlipper viewFlipper;
ImageButton myButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.herkunft_rind);

viewFlipper = (ViewFlipper) findViewById(R.id.herkunft_rinder_view_flipper);

ImageButton imageButton = null;
imageButton = (ImageButton) findViewById(R.id.button_1);
imageButton.setOnClickListener(this);
imageButton = (ImageButton) findViewById(R.id.button_2);
imageButton.setOnClickListener(this);
imageButton = (ImageButton) findViewById(R.id.button_3);
imageButton.setOnClickListener(this);
imageButton = (ImageButton) findViewById(R.id.button_4);
imageButton.setOnClickListener(this);
}
public void setAnimationFade(int id) {
myButton = (ImageButton) findViewById(id);
ObjectAnimator animator = ObjectAnimator.ofFloat(myButton, View.ALPHA, 0.5f, 1f);
animator.setDuration(300); //ms
animator.start();
animator.addListener(this);
}
@Override
public void onAnimationStart(Animator animation) {
myButton.setAlpha(1f);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button_1: {
setAnimationFade(R.id.button_1);
text = (TextView) findViewById(R.id.button_1_text_1);
text.setVisibility(View.VISIBLE);
text = (TextView) findViewById(R.id.button_1_text_2);
text.setVisibility(View.VISIBLE);
break;
}
case R.id.button_2: {
setAnimationFade(R.id.button_2);
text = (TextView) findViewById(R.id.button_2_text_1);
text.setVisibility(View.VISIBLE);
text = (TextView) findViewById(R.id.button_2_text_2);
text.setVisibility(View.VISIBLE);
break;
}
case R.id.button_3: {
setAnimationFade(R.id.button_3);
text = (TextView) findViewById(R.id.button_3_text_1);
text.setVisibility(View.VISIBLE);
text = (TextView) findViewById(R.id.button_3_text_2);
text.setVisibility(View.VISIBLE);
break;
}
case R.id.button_4: {
setAnimationFade(R.id.button_4);
text = (TextView) findViewById(R.id.button_4_text_1);
text.setVisibility(View.VISIBLE);
text = (TextView) findViewById(R.id.button_4_text_2);
text.setVisibility(View.VISIBLE);
break;
}

我不想的是直接在XML文件中定义它。我可以缩短它吗?

最佳答案

是的,你可以,我个人经常这样做。
您可以在xml布局中调用该方法。

即:

<Button
android:onClick="myMethod()"
/>

注意 myMethod() 必须有一个像这样的签名

public void myMethod(View v)

在 myMethod 中,您可能想要辨别哪个是您点击的 View (也可以是异构)并相应地采取行动。

只需在使用 v.getId() 的地方添加一个 switch() 来确定触发事件的 View 。

像这样:

switch(v.getId())
{
case R.id.txtPhone:
{
// Do something
// ...
break;
}
case R.id.txtMenu:
{
// Show options menu
// ...
break;
}
// ...
default:
{
break;
}
}

关于java - 安卓按钮 : Shorten OnClick method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37259231/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com