创建一个按钮来执行两个单击操作



我在一个活动上有一组按钮。当我点击一个按钮时它会在textview上设置一个文本它会改变按钮的背景颜色。然后,当我按下相同的按钮时,它将转到下一个活动。下面的代码执行这些操作。我已经改变了按钮的文本,并做第二次点击动作。有没有更好的方法在不改变文本的情况下做到这一点?

在代码中,我已经显示了1个按钮。我对其他按钮也是这样做的。

活动
public class Activity2 extends ActionBarActivity implements View.OnClickListener {
Button R1btn, R2btn, R3btn;
private TableLayout tableLayout;
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);
        tableLayout=(TableLayout)findViewById(R.id.tl);
    R1btn = (Button) findViewById(R.id.button1);
    R1btn.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
    switch(view.getId()){
    case R.id.button1:
      Button button1 = (Button)view;
      String btnText = button1.getText().toString();
      if (btnText.equals("1")) {
        R1btn.setBackgroundColor(Color.RED); 
        R2btn.setBackgroundColor(Color.GREEN)); 
        R3btn.setBackgroundColor(Color.GREEN)); 
        TextView rTV  = (TextView) findViewById(R.id.rTV);
            rTV.setText(R1);
        button1.setText("1 ");  
}   else if (btnText.equals("1 ")) {
            Intent intent = new Intent(Activity2.this, Activity3.class);
            startActivity(intent);
        button1.setText("1");
    break;
}
}
}

通过使用按钮的标签,您将能够实现您的目标。示例代码如下:

   Button button1 = (Button)view;
      String btnTag= button1.getTag().toString();
// For First Time when button is clicked
      if (btnTag.equals("") || btnTag.equals("CLICKED2")) {
        R1btn.setBackgroundColor(Color.RED); 
        R2btn.setBackgroundColor(Color.GREEN)); 
        R3btn.setBackgroundColor(Color.GREEN)); 
        button1.setTag("CLICKED1");  
      }   else if (btnTag.equals("CLICKED1")) {
        button1.setTag("CLICKED2");  
        Intent intent = new Intent(Activity2.this, Activity3.class);
        startActivity(intent);
      }
break;

使用

 button = (Button) findViewById(R.id.button_view);
button .setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        shortclick();
    }
 });
button .setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
    longclick();
    return true;
}

});

public void shortclick()

{吐司。makeText:"你为什么那样做?", Toast.LENGTH_LONG).show();

}

public void longclick()

public void shortclick()

和其他单键点击

您可以定义静态布尔变量,这些变量将映射到相应的按钮。单击按钮时,使变量为真。根据变量值,您可以执行操作。无需更改按钮的文本

//初始化值

boolean isButton1 = false;

//点击方法

@Override
    public void onClick(View view) {
    switch(view.getId()){
    case R.id.button1:
    isButton1 != isButton1;
      if (isButton1) {
        R1btn.setBackgroundColor(Color.RED); 
        R2btn.setBackgroundColor(Color.GREEN)); 
        R3btn.setBackgroundColor(Color.GREEN)); 
        TextView rTV  = (TextView) findViewById(R.id.rTV);
            rTV.setText(R1);
    }   else  {
            Intent intent = new Intent(Activity2.this, Activity3.class);
            startActivity(intent);
    }
    break;
}
}

这是一个例子,有人创建了自己的Button类,从播放图标更改为暂停图标。

这是一个非常好的方法,因为所有的代码都在一个中心位置。

https://github.com/CyanogenMod/android_packages_apps_Apollo/blob/085099c04708d75380b1c996094dbdc7dc0a11a0/src/com/andrew/apollo/widgets/PlayPauseButton.java

你只需要添加一个静态布尔值来"记住"如果按钮现在处于"播放或暂停"

然后您可以检查它(例如在方法UpdateState中)并根据当前状态更改外观。

static boolean isPlay = true;
public void updateState() {
        if (isPlay) {
            //Change appearance
            isPlay = false;
        } else {
            //Change appearance
            isPlay=true;
        }
    }

最新更新