简体   繁体   中英

How to hide one item(Button) and show another item(Button) from menu (Buttons in ActionBar) in single Activity

在此处输入图片说明

As attached in screenshot when I click on edit button then i want to hide edit button and show save button. How can i do that?

My menu file is as below:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
    android:id="@+id/edit_button"
    android:icon="@drawable/edit_button"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/edit"/> 

</menu>

Is it possible this with image as edit to another image as save when button is clicked once and as save to edit when button is clicked again.

final DatePicker dp2 = (DatePicker) findViewById(R.id.datePick2);
final Button btn2 = (Button) findViewById(R.id.btnDate2);

dp2.setVisibility(View.GONE);

or

dp2.setVisibility(View.INVISIBLE);

btn2.setVisibility(View.GONE);

or

btn2.setVisibility(View.INVISIBLE);

when need visible:

btn2.setVisibility(View.VISIBLE);

or you use Invisible or Gone, but not both!

Try this

 @Override
    public boolean onOptionsItemSelected(MenuItem item){
    {

            if(editing){
                   item.setIcon(R.drawable.ic_save);
            }else{
                   item.setIcon(R.drawable.ic_edit);
            }   


        return super.onOptionsItemSelected(menu);

    }

Based on your comments, you need other than asked.

Make not relevant, but change the

<item
    android:id="@+id/edit_button"

to

<item
    android:id="@+id/edit_or_save_button"

From the name you should know what will be the next:

if you press the button when it was an Edit, than you will do stuff in the activity which allows editing your data, but this button will change his text ( and action) to Save!

Of course you will assign different action listener, which will do the validation and save action.

If you really want to stick with 2 button idea (highly disagree with that) :

Button edit_button = (Button)findViewById(R.id.edit_button);
Button save_button = (Button)findViewById(R.id.save_button);

...

edit_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        edit_button.setVisibility(View.INVISIBLE);
        save_button.setVisibility(View.INVISIBLE);

        DoEdit(v);
    }
});


 save_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        save_button.setVisibility(View.INVISIBLE);
        edit_button.setVisibility(View.INVISIBLE);

        DoValidationAndSave(v);
    }
});

I hope it solve your problem!

Try this,

public boolean checkHide = false
@Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()){
        case EDIT:
            if(checkHide){
                checkHide=false;
                item.setTitle("edit");
                  // ToDo your function

                }
            else{
                checkHide=true;
                item.setTitle("save");
                  // ToDo your function
                 }
}

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