我对android很新,这是我第一天这样做。我正在创建这个有2个按钮的应用程序,负责在按下后更改布局的颜色。按下按钮时,我使用Toast类显示一条消息,说明颜色已更改。 但我希望逻辑能够使特定按钮的toast消息只显示一次,而不是后续按下相同按钮。为此,我使用了一个布尔标志,只要按钮是单击,将布尔变量更改为false,然后不显示Toast消息。但是这个逻辑存在问题。该应用程序不会显示toast消息以供将来点击使用。我真的很感激,如果有人能帮助我一点点这个逻辑,那么应用程序会知道足够聪明,可以处理未来的印刷机。
public class MainActivity extends AppCompatActivity {
private Button myblue_button;
private Button mygreen_button;
private LinearLayout backGround;
private TextView textArea;
boolean forBLue = true;
boolean forGreen = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textArea = (TextView) findViewById(R.id.textID); //id for text area to later change it
backGround = (LinearLayout) findViewById(R.id.linearlayoutID);
myblue_button = (Button) findViewById(R.id.blue_button);
myblue_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
textArea.setText("Now we feel blue");
if (forBLue == true){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
forBLue = false;
}
}
});
mygreen_button = (Button) findViewById(R.id.green_button);
mygreen_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
textArea.setText("Now we feel green");
if (forGreen == true) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
forGreen = false;
}
}
});
}
}
以下是XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayoutID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id = "@+id/textID"
android:text="Click the button that best represents your mood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<Button
android:id="@+id/blue_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BLUE"
android:layout_weight="1"
/>
<Button
android:id="@+id/green_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GREEN"
android:layout_weight="1"
/>
</LinearLayout>>
答案 0 :(得分:1)
private int blue, green = 0;
myblue_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
textArea.setText("Now we feel blue");
if (blue == 0){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
blue = 1;
green = 0;
}
}
});
mygreen_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
textArea.setText("Now we feel green");
if (green == 0) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
green = 1;
blue = 0;
}
}
});
}
}
这是一个可以解决问题的代码。
答案 1 :(得分:1)
你需要这样做:
if (forBLue == true){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
forBLue = false;
forGreen = true ;//edit here
}
和:
if (forGreen == true) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
forGreen = false;
forBlue = true;//edit here
}
答案 2 :(得分:0)
这样做:
boolean shouldShowToastOnBlue = true;
boolean shouldShowToastOnGreen = true;
...
myblue_button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_bright));
textArea.setText("Now we feel blue");
if (shouldShowToastOnBlue){
Toast.makeText(MainActivity.this,"Now we feel blue",Toast.LENGTH_SHORT).show();
shouldShowToastOnBlue = false;
}
shouldShowToastOnGreen = true;
}
});
mygreen_button = (Button) findViewById(R.id.green_button);
mygreen_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
backGround.setBackgroundColor(getResources().getColor(android.R.color.holo_green_light));
textArea.setText("Now we feel green");
if (shouldShowToastOnGreen) {
Toast.makeText(MainActivity.this, "Now we feel green", Toast.LENGTH_SHORT).show();
shouldShowToastOnGreen = false;
}
shouldShowToastOnBlue = true;
}
});