Set the state of the button and save it after pressing the key

I am not sure if this is possible. I have this button:

<Button 
android:id="@+id/b1" 
android:layout_width="wrap_content" 
android:layout_height="45px"
android:drawableTop="@drawable/buttontv"
android:layout_weight="1"
android:background="@null"
android:textColor="#ffffff"
android:textSize="9px"
android:text="TV"/>

And this button has this xml element:

<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
 android:state_focused="true"
 android:state_pressed="false"
 android:drawable="@drawable/tv" />
<item
 android:state_focused="true"
 android:state_pressed="true"
 android:drawable="@drawable/tv_pressed" />
<item
 android:state_focused="false"
 android:state_pressed="true"
 android:drawable="@drawable/tv_pressed" />
<item  
 android:drawable="@drawable/tv" />
</selector>

And in my application, I use this code to click a button:

            OnClickListener b1Listener = new OnClickListener(){             
                    @Override
                    public void onClick(View v) {   loadUrl("http://example.org/"); v.setPressed(true); }
            };
            Button b1 = (Button) findViewById(R.id.b1);
            b1.setOnClickListener(b1Listener);

What I would like, when I clicked the button, drawableTop sets one of the elements with the @ drawable / tv_pressed attribute - and remains there (as in "this is the current active button").

I tried adding v.setPressed (true) to the onClick function (since that is all I could find on Google), but that did not work.

Can this be done? Or is there a better alternative to what I'm trying to accomplish?

+3
source share
4 answers

If you need a button that will be pressed and remains active, use ToggleButton

+5

fooobar.com/questions/1793904/...

    button.setFocusable(true);
    button.setFocusableInTouchMode(true);///add this line

. xml

    <item name="android:focusable">true</item>
    <item name="android:focusableInTouchMode">true</item>
+1

, toggle, listview , togglebutton listview setOnItemClickListener. , listview. , .

0

/ ? , , , .:)

1- DialogFragment  , Onstart().

@Override
public void onStart() {
super.onStart();
final AlertDialog D = (AlertDialog) getDialog();
if (D != null) {
    Button positive = (Button) D.getButton(Dialog.BUTTON_POSITIVE);
    positive.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            if (edittext.equals("")) {
  Toast.makeText(getActivity(), "EditText empty don't close",Toast.LENGTH_SHORT).show();
            } else {
            D.dismiss(); //dissmiss dialog
            }
        }
    });
}}
0

Source: https://habr.com/ru/post/1793903/


All Articles