package com.example.scrolltes1;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.KeyEventDispatcher;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import static android.graphics.Color.BLUE;
import static android.graphics.Color.GRAY;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
boolean constrain = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = findViewById(R.id.button1);
Button button2 = findViewById(R.id.button2);
Button button3 = findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Toast.makeText(this, "Button 1 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button2:
Toast.makeText(this, "Button 2 clicked", Toast.LENGTH_SHORT).show();
break;
case R.id.button3:
Toast.makeText(this, "Button 3 clicked", Toast.LENGTH_SHORT).show();
break;
}
}
}
would it just be possible to replace everything inside of the Toast.makeText parenthesis with something that changes background color?
you could create a custom layout with a specific color for each button:
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1
Toast ToastMessage = Toast.makeText(this,"Button 1 clicked", Toast.LENGTH_SHORT);
View toastView = ToastMessage.getView();
toastView.setBackgroundResource(R.layout.custom_color);
ToastMessage.show();
break;
// more cases
}
}
See this article for more details about the solution: https://www.android-examples.com/change-toast-message-background-color-in-android/
If this helps, don't hesitate to accept it as an answer:)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.