0

我有一个引用按钮的 java 文件:

public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState) {
        createMenus();
        return inflater.inflate(R.layout.fragment_test, container, false);
    }

@Override
    public void onClick(final View view) {
        final int actionBarHeight = getActivity().findViewById(R.id.title_main_container).getHeight();
        switch (view.getId()) {
            case R.id.news_article_menu_button:
                mOptionsMenuHelper.showMenu(view.getBottom() + actionBarHeight, Gravity.RIGHT);
                break;
            case R.id.text_size:
                mTextSize = TextSize.values()[(mTextSize.ordinal() + 1) % TextSize.values().length];
                updateFontSize();
                break;
            default:
                throw new IllegalArgumentException("Invalid view Id" + view.getId());
        }
    }
private void setOnclickListeners(final View view){
        final ImageButton button = (ImageButton) view.findViewById(R.id.menu_button);
        button.setOnClickListener(this);
    }

    private void createMenus() {
        mPreferenceMenuItems.add(PreferenceMenuItems.MENU_PREFERENCES);
        mPreferencesMenuHelper = new MenuHelper(getActivity(), mPreferenceMenuItems,this);

        mDeleteMenuItems.add(DeleteMenuItems.DELETE_PREFERENCES);
        mDeleteMenuHelper = new MenuHelper(getActivity(), mDeleteMenuItems,this);
    }

这是定义选择器的相应按钮“over_btn”:

<?xml version="1.0" encoding="utf-8"?>


    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_enabled="false"
        android:drawable="@drawable/over_pressed" />
    <item
        android:state_pressed="true"
        android:state_enabled="true"
        android:drawable="@drawable/over_pressed" />
    <item
        android:state_focused="true"
        android:state_enabled="true"
        android:drawable="@drawable/over_pressed" />
    <item
        android:state_enabled="true"
        android:drawable="@drawable/over" />
    </selector>

这是使用相同fragment_test的相应xml文件之一:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_alignParentTop="true"
    android:clickable="true"
    android:background="@color/background_bar" >

    <ImageButton
        android:id="@+id/menu_button"
        android:layout_width="49dp"
        android:layout_height="48.5dp"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:background="@android:color/transparent"
        android:gravity="right"
        android:paddingRight="14dp"
        android:src="@drawable/over_btn" />

</RelativeLayout>

I want the button to switch to a different image onclick and stay in that state till pressed again instead of highlighting once when pressed, is there any way to go about this?

Thanks! Justin

4

2 回答 2

0

添加

<item 
     android:state_selected="true" 
     android:drawable="@drawable/info_selected" />

到您的selector,当您单击 ImageButton 时,调用view.setSelected(!view.isSelected())以在选中和未选中状态之间切换

public void onClick(final View view) {
          switch (view.getId()) {
               case R.id.imageButtonId:
               view.setSelected(!view.isSelected());
               break;
          }

 }
于 2013-07-10T15:32:13.240 回答
0

您应该可以使用 onClick 侦听器来执行此操作,如本文所示。//这些是实例变量 private ImageButton button = null; 私有布尔 imageSwitched = false;

//call this method in your onActivityCreated() method. If the
//layout containing the button is not yet inflated you will not
//be able to find your image button. 
private void initializeButton(){     
    button= (ImageButton)getActivity().findViewById(R.id.imgButton);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if(!imageSwitched){
                button.setBackgroundResource(R.drawable.a);
                imageSwitched = true;
            } else {
                buttons.setBackgroundResource(R.drawable.b);
                imageSwitched = false;
            }
        }
    });
}

要切换 2 个视图,您需要使用一些状态变量。我选择了在单击时设置的布尔值来处理单击时切换到的图像。

此外,如果您不想使用侦听器,您可以将按钮设置为在 XML 中具有 onClick 方法并使用相同的状态处理理念

public void buttonSwitch(View v){
 //state handling here
}

这样,该方法仅在单击时调用,并且不使用侦听器的开销。

于 2013-07-10T15:36:40.320 回答