简体   繁体   中英

How to create a class to display toast message once when image button clicked?

I have more than one image button on my app and I want it to display a toast message when each buttons are clicked. However in my code I don't like the idea of it having continous toast message set for each image button over and over again. Is there a way to shorten these messages like in a class if so how can I do it? Note: The only one I dont want the toast message to appear on is the room service button.

My code:

public class MainMenu extends AppCompatActivity {

private ImageButton roomservice, transportation, foodservice, maintenance, checkout, baggagecollection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);
    getSupportActionBar().setTitle("MainMenu");

    roomservice = (ImageButton) findViewById(R.id.roomservice);
    transportation = (ImageButton) findViewById(R.id.transportation);
    foodservice = (ImageButton) findViewById(R.id.foodservice);
    maintenance = (ImageButton) findViewById(R.id.maintenance);
    checkout = (ImageButton) findViewById(R.id.checkout);
    baggagecollection = (ImageButton) findViewById(R.id.baggagecollection);

    roomservice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            openRoomServiceMenu(); // creates main menu method name
        }
    });

    transportation.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainMenu.this, "This action is unavailable", Toast.LENGTH_LONG).show();
        }
    });

    foodservice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainMenu.this, "This action is unavailable", Toast.LENGTH_LONG).show();
        }
    });

    maintenance.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainMenu.this, "This action is unavailable", Toast.LENGTH_LONG).show();
        }
    });

    checkout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainMenu.this, "This action is unavailable", Toast.LENGTH_LONG).show();
        }
    });

    baggagecollection.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainMenu.this, "This action is unavailable", Toast.LENGTH_LONG).show();
        }
    });

}

I wouldn't suggest to create entire new class of ImageView if it just shows toast when its clicked. what you can do is to create new type of click Listener which will implement View.OnClickListener and take string or string resource as constructor parameter. after that override onClick In your new click listener and show toast there. in the end set your view's click listener your newly created class and pass string in constructor you want to show in toast. that way you wont be forced to create new type of class if you want to have same functionality with buttons or other types of views.

You can create method inside your Activity

void handleClickWithToast(String message, int duration) {
    Toast.makeText(MainMenu.this, message, length).show();
}

or even shorter

void handleClickWithLongToast(String message) {
    Toast.makeText(MainMenu.this, message, Toast.LENGTH_LONG).show();
}

and call it inside click listener

transportation.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        handleClickWithLongToast("This action is unavailable");
    }
});

or You can create utils class with static methods

class ToastUtils {
    public static void showShortToast(Context context, String message) {
        showToast(context, message, Toast.LENGTH_SHORT);
    }

    public static void showLongToast(Context context, String message) {
        showToast(context, message, Toast.LENGTH_LONG);
    }

    private static void showToast(Context context, String message, int duration) {
        Toast.makeText(context, message, duration).show();
    }
}

You can also create custom View.ClickListener

public class LongToastClickListener implements View.OnClickListener {

    private String message;

    LongToastClickListener(String message) {
        this.message = message;
    }

    @Override
    public void onClick(View view) {
        ToastUtils.showLongToast(view.getContext(), message);
    }
}

and apply it on your buttons

button.setOnClickListener(new LongToastClickListener("Message"));

You can try one of this way to reduce your repetitive code

Simply create a separate class and define static method on that class to show toast

public class CommonUtils {
 public static void showToast(Context context,String message){
    Toast.makeText(context, message, Toast.LENGTH_LONG).show();
    }
}

Then call this method from Activity/Fragment

CommonUtils.showToast(this,"Your Message");

This is a good practice to Not To Repeat Yourself

so You can pass an object of a custom listener class

private class MyListener implements View.OnClickListener // make an inner class
{ 
  String msg;
  MyListener(String msg){this.msg = msg;} // if you want separate msg for each image , otherwise omit the constructor 
  @Override
  public void onClick(View view){
  Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show(); 
  }
}

Now set it to Button like

maintainance.setOnClickListener(new MyListener("your msg"));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM