在按钮单击时显示和隐藏下拉列表?

问题描述:

我正在使用按钮,单击它会打开一个下拉列表,并且箭头符号会更改.但是我想再次隐藏按钮单击时的下拉列表.表示要交替显示和隐藏按钮单击.我正在使用此代码.

I am using a button and on clicking it ,a drop down list opens and the arrow sign changes.but I want to hide the drop down list on button's click again.mean to show and hide alternately on button's click. i am using this code.

    protected void onCreate(Bundle savedInstanceState) {
    myphotosBtn=(Button)findViewById(R.id.myPhotosBtn);
    myphotosBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        arrowDown.setImageResource(R.drawable.arrow_up);
       findViewById(R.id.dropdownList).setVisibility(View.VISIBLE);
        }
    });

请帮助.

您可以使用布尔值.

让我们说..

    boolean isButton=true;


     protected void onCreate(Bundle savedInstanceState) {
        myphotosBtn=(Button)findViewById(R.id.myPhotosBtn);
        myphotosBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


            if(isButton){
                        arrowDown.setImageResource(R.drawable.arrow_up);
                       findViewById(R.id.dropdownList).setVisibility(View.VISIBLE);
                       isButton=false;
            }else{
                        arrowDown.setImageResource(R.drawable.down);
                       findViewById(R.id.dropdownList).setVisibility(View.GONE);
                       isButton=true;
                 }
            }
        });