Android composite control Textview + Switch - click method

Asked

Viewed 95 times

0

I made a layout of a Textview + Switch button. How do I set the onClick method in prefs_activity.xml and receive notification in the class implementation?

layout/preference_switch.xml

<TextView
    android:id="@+id/switchLabel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:fontFamily="sans-serif-light"
    android:textSize="14sp"
    android:layout_gravity="left|center"/>

<Switch
    android:id="@+id/switchButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right|center"/>

values/lib_attr.xml

 <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <declare-styleable name="lib">
            <attr name="title" format="string"/>
            <attr name="label" format="string"/>
            <attr name="summary" format="string"/>
        </declare-styleable>
    </resources>

Cpreferenceswitch.java

public class CPreferenceSwitch extends FrameLayout
{
  private TypedArray m_params;

  private TextView m_label;
  private Switch m_switch;


  public CPreferenceSwitch(Context context)
  {
    this(context,null);
  }

  public CPreferenceSwitch(Context context, AttributeSet attrs)
  {
    this(context, attrs, 0);
  }

  public CPreferenceSwitch(Context context, AttributeSet attrs, int defStyleAttr)
  {
    super(context, attrs, defStyleAttr);

    m_params = context.obtainStyledAttributes(attrs,R.styleable.lib);

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    inflater.inflate(R.layout.preference_switch, this);
  }


  @Override
  protected void onFinishInflate()
  {
    super.onFinishInflate();

    m_label = (TextView) findViewById(R.id.switchLabel);
    m_switch = (Switch) findViewById(R.id.switchButton);

    m_label.setText(m_params.getString(R.styleable.lib_label));

    m_params.recycle();
  }

}

prefs_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:lib="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <lib.client.control.CPreferenceCategory
            style="@style/preference_category"
            lib:title="Receber notificação"
            lib:summary="Notificações enviadas para a área de menu do aplicativo">
        </lib.client.control.CPreferenceCategory>

        <lib.client.control.CPreferenceSwitch
            style="@style/preference_switch"
---------> 
--------->                android:onClick="swichClick"  <------------
---------> 
            lib:label="Sugestão de categoria">
        </lib.client.control.CPreferenceSwitch>

        <lib.client.control.CPreferenceSwitch
            style="@style/preference_switch"
            lib:label="Sugestão de indicador">
        </lib.client.control.CPreferenceSwitch>

        <lib.client.control.CPreferenceSwitch
            style="@style/preference_switch"
            lib:label="Sugestão de melhoria no aplicativo">
        </lib.client.control.CPreferenceSwitch>

        <lib.client.control.CPreferenceSwitch
            style="@style/preference_switch"
            lib:label="Solicitação de ajuda">
        </lib.client.control.CPreferenceSwitch>

        <lib.client.control.CPreferenceSwitch
            style="@style/preference_switch"
            lib:label="Relato de erro">
        </lib.client.control.CPreferenceSwitch>

    </LinearLayout>

</ScrollView>

Print screen

inserir a descrição da imagem aqui

  • Why don’t you use the API Preference Android makes available to manage app settings .

  • I couldn’t make it work, see my post - http://answall.com/questions/156018/android-erro-must-specify-preferencetheme-in-theme

1 answer

0

You need to add one Listener. It is not necessary to put the onClick in XML.

example:

CPreferenceSwitch mySwitch = (CPreferenceSwitch) findViewById(R.id.id_do_switch);

mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked){
            //faz algo quando selecionado
        }else{
           //faz algo quando não está selecionado
        }
    }
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.