手风琴菜单的Android背景颜色

时间:2016-03-16 14:19:52

标签: java android button background

我希望Java文件中的代码设置所点击的任何按钮的背景颜色,并保留它,即使你放开按钮也是如此。提前致谢

我真的没有在这里得到格式。我希望它清楚属于哪里。

爪哇:

 package mika.actual;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class accordion extends Activity {
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.accordian);

        Button btnProfile = (Button) findViewById(R.id.btnProfile);
        Button btnSettings = (Button) findViewById(R.id.btnSettings);
        Button btnPrivacy = (Button) findViewById(R.id.btnPrivacy);

        View panelProfile = findViewById(R.id.panelProfile);
        panelProfile.setVisibility(View.GONE);

        View panelSettings = findViewById(R.id.panelSettings);
        panelSettings.setVisibility(View.GONE);

        View panelPrivacy = findViewById(R.id.panelPrivacy);
        panelPrivacy.setVisibility(View.GONE);

        btnProfile.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // DO STUFF
                View panelProfile = findViewById(R.id.panelProfile);
                panelProfile.setVisibility(View.VISIBLE);

                View panelSettings = findViewById(R.id.panelSettings);
                panelSettings.setVisibility(View.GONE);

                View panelPrivacy = findViewById(R.id.panelPrivacy);
                panelPrivacy.setVisibility(View.GONE);

            }
        });

        btnSettings.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // DO STUFF
                View panelProfile = findViewById(R.id.panelProfile);
                panelProfile.setVisibility(View.GONE);

                View panelSettings = findViewById(R.id.panelSettings);
                panelSettings.setVisibility(View.VISIBLE);

                View panelPrivacy = findViewById(R.id.panelPrivacy);
                panelPrivacy.setVisibility(View.GONE);

            }
        });

        btnPrivacy.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // DO STUFF
                View panelProfile = findViewById(R.id.panelProfile);
                panelProfile.setVisibility(View.GONE);

                View panelSettings = findViewById(R.id.panelSettings);
                panelSettings.setVisibility(View.GONE);

                View panelPrivacy = findViewById(R.id.panelPrivacy);
                panelPrivacy.setVisibility(View.VISIBLE);

            }
        });

    }

}

XML:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFFFF"
    android:orientation="vertical">

    <Button
        android:id="@+id/btnProfile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Profile"
        android:textColor="#FFFFFFFF" />

    <LinearLayout
        android:id="@+id/panelProfile"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFFFFFFF">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#FFFFFFFF">

                <LinearLayout
                    android:id="@+id/panelProfile1"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strName"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Name" />

                    <EditText
                        android:id="@+id/txtName"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>


                <LinearLayout
                    android:id="@+id/panelProfile2"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strSurname"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Surname" />

                    <EditText
                        android:id="@+id/txtSurname"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

            </LinearLayout>

        </ScrollView>

    </LinearLayout>



    <Button
        android:id="@+id/btnSettings"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Settings"
        android:textColor="#FFFFFFFF" />

    <LinearLayout
        android:id="@+id/panelSettings"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="#FFFFFFFF">

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <LinearLayout
                    android:id="@+id/panelSettings1"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strMail"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="e-mail" />

                    <EditText
                        android:id="@+id/txtMail"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/panelSettings2"
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="#FFFFFFFF">

                    <TextView
                        android:id="@+id/strPhone"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Phone" />

                    <EditText
                        android:id="@+id/txtPhone"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />

                </LinearLayout>

            </LinearLayout>

        </ScrollView>

    </LinearLayout>



    <Button
        android:id="@+id/btnPrivacy"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Privacy"
        android:textColor="#FFFFFFFF" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <LinearLayout
            android:id="@+id/panelPrivacy"
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#FFFFFFFF">

            <CheckBox
                android:id="@+id/checkFacebook"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Facebook"
                android:textColor="#ff355689">
            </CheckBox>
            <CheckBox
                android:id="@+id/checkLinkedIn"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="LinkedIn"
                android:textColor="#ff355689">
            </CheckBox>
            <CheckBox
                android:id="@+id/checkTwitter"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Twitter"
                android:textColor="#ff355689">
            </CheckBox>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您要做的是,当单击按钮并展开其子菜单时,更改该按钮的背景,直到再次单击它。您可以使用按钮上的标记执行此操作:

btnSettings.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // DO STUFF
        //if button is not selected, change background and tag the View as selected
        if(v.getTag() == null || v.getTag().equals("not_selected")) {
            v.setBackgroundColor(Color.RED);
            v.setTag("selected");
            //show sub-menu
        }
        //if button is already selected, reset background and tag the View as not selected
        else{
            v.setBackgroundResource(android.R.drawable.btn_default);
            v.setTag("not_selected");
            //hide sub menu
        }

    }
});

请注意v.setBackgroundResource(android.R.drawable.btn_default);正在将Button背景重置为android drawable。您可能希望将其更改为自定义可绘制或颜色。 您可以使用ExpandableListView来实现此类菜单。

答案 1 :(得分:0)

button.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    view.setBackgroundColor(ContextCompat.getColor(context, R.color.presed_color));
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    view.setBackgroundColor(ContextCompat.getColor(context, R.color.unpresed_color));
                }
                return false;
            }
        });