[英]Android button first and second click
我需要有关按钮第一次和第二次单击的帮助。 第一次单击应该发送一条短信并更改按钮背景颜色和按钮文本颜色。 第二次单击应发送其他短信并设置返回按钮背景色和文本颜色。 谢谢。
public boolean onLongClick(View v) {
switch (v.getId()){
case R.id.button1:
btn1.setBackgroundColor(Color.GREEN);
btn1.setTextColor(Color.RED);
sendSMSMessage1();
// On second click do other event
sendSMSMessage11();
btn1.setBackgroundColor(Color.BLACK);
btn1.setTextColor(Color.WHITE);
break;
case R.id.button2:
btn2.setBackgroundColor(Color.GREEN);
btn2.setTextColor(Color.RED);
sendSMSMessage2();
// On second click do other event
sendSMSMessage22();
btn2.setBackgroundColor(Color.BLACK);
btn2.setTextColor(Color.WHITE);
break;
}
return false;
}
xml
<Button
android:id="@+id/button1"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="214dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_marginStart="123dp"
android:layout_marginEnd="124dp"
android:layout_marginBottom="372dp"
android:text="START"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="#ffffff"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.521"
app:layout_constraintStart_toStartOf="parent"
app:strokeColor="#ffffff"
app:strokeWidth="5dp" />
您可以通过附加标志来做到这一点。 例如
boolean wasButtonClicked = false;
button.setOnLongClickListener {
if (wasButtonClicked) {
//action 2
} else {
//action1
}
}
这种方式行不通。
case R.id.button1:
boolean wasButtonClicked = false;
if (wasButtonClicked) {
btn1.setBackgroundColor(Color.GREEN);
btn1.setTextColor(Color.RED);
sendSMSMessage1();
}
else {
btn1.setBackgroundColor(Color.BLACK);
btn1.setTextColor(Color.WHITE);
}
break;
试试这个代码片段:
boolean buttonClicked = false; //click track for button 1
boolean buttonClicked2 = false; //click track for button 2
public boolean onLongClick(View v) {
switch (v.getId()){
case R.id.button1:
buttonClicked = !buttonClicked;
if(buttonClicked){
btn1.setBackgroundColor(Color.GREEN);
btn1.setTextColor(Color.RED);
sendSMSMessage1();
}else{
sendSMSMessage11();
btn1.setBackgroundColor(Color.BLACK);
btn1.setTextColor(Color.WHITE);
}
break;
case R.id.button2:
buttonClicked2 = !buttonClicked2;
//do similar stuff
break;
}
return true;
}
also in your xml of your button you may need to add onClick attribute so that it handles your onClick if you have not done by your java code.
<Button
android:id="@+id/button1"
style="@style/Widget.Material3.Button.OutlinedButton"
android:layout_width="214dp"
android:layout_height="70dp"
android:layout_centerHorizontal="true"
android:layout_marginStart="123dp"
android:layout_marginEnd="124dp"
android:layout_marginBottom="372dp"
android:text="START"
android:textAlignment="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textColor="#ffffff"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.521"
app:layout_constraintStart_toStartOf="parent"
app:strokeColor="#ffffff"
//add your onClick android:onClick="onLongClick" //or app:onClick = "onLongClick"
//one should work as you have mentioned on click not long click in your question
app:strokeWidth="5dp" />
像魅力一样工作,非常感谢大家:
boolean buttonClicked = false; //点击按钮1的轨道 boolean buttonClicked2 = false; //点击按钮2的轨道
公共 boolean onLongClick(查看 v){
switch (v.getId()){
case R.id.button1:
buttonClicked = !buttonClicked;
if(buttonClicked){
btn1.setBackgroundColor(Color.GREEN);
btn1.setTextColor(Color.RED);
sendSMSMessage1();
}else{
sendSMSMessage11();
btn1.setBackgroundColor(Color.BLACK);
btn1.setTextColor(Color.WHITE);
}
break;
case R.id.button2:
buttonClicked2 = !buttonClicked2;
//do similar stuff
break;
}
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.