按下按钮显示列表并在压制时消失?

时间:2018-01-18 17:36:58

标签: android

我需要在我的布局中创建一个按钮,在按下时会在其下方显示一个列表并在按下它时列表应该消失?我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

这种列表是弹出菜单或上下文菜单。

import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;

public class MyLovelyClass extends Activity {

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lovely_layout);

        //get a reference to the view for pressing
        TextView pressView = (TextView)findViewById(R.id.press);

                //register it for context
        registerForContextMenu(pressView);
    }

    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.lovely_context, menu);
    }

    public boolean onContextItemSelected(MenuItem item) {
            //find out which menu item was pressed
        switch (item.getItemId()) {
            case R.id.option1:
                doOptionOne();
                return true;
            case R.id.option2:
                doOptionTwo();
                return true;
            default:
                return false;
        }
    }

    private void doOptionOne() {
        Toast.makeText(this, "Option One Chosen...", Toast.LENGTH_LONG).show();
    }

    private void doOptionTwo() {
        Toast.makeText(this, "Option Two Chosen...", Toast.LENGTH_LONG).show();     
    }
}

或者在ListFragment中,您可以使用此示例

@Override
public void onCreateContextMenu(final ContextMenu menu, final View v,
                                final ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    final StartConversationActivity activity = (StartConversationActivity) getActivity();
    activity.getMenuInflater().inflate(mResContextMenu, menu);
    final AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
    if (mResContextMenu == R.menu.conference_context) {
        activity.conference_context_id = acmi.position;
    } else if (mResContextMenu == R.menu.contact_context) {
        activity.contact_context_id = acmi.position;
        final Contact contact = (Contact) activity.contacts.get(acmi.position);
        final MenuItem blockUnblockItem = menu.findItem(R.id.context_contact_block_unblock);
        final MenuItem showContactDetailsItem = menu.findItem(R.id.context_contact_details);
        if (contact.isSelf()) {
            showContactDetailsItem.setVisible(false);
        }
        XmppConnection xmpp = contact.getAccount().getXmppConnection();
        if (xmpp != null && xmpp.getFeatures().blocking() && !contact.isSelf()) {
            if (contact.isBlocked()) {
                blockUnblockItem.setTitle(R.string.unblock_contact);
            } else {
                blockUnblockItem.setTitle(R.string.block_contact);
            }
        } else {
            blockUnblockItem.setVisible(false);
        }
    }
}

答案 1 :(得分:0)

您应该使用的是切换按钮。

它有两种状态(ON和OFF)。

在ON状态下,您可以显示列表,在OFF状态下,您可以隐藏列表。

要隐藏或使列表可见,请使用:

list.setVisibility(View.INVISIBLE) 
list.setVisibility(View.VISIBLE) 

处理Toggle按钮的ON和OFF状态

toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    //ON state
                } else {
                    //OFF state
                }
            }
        });