简体   繁体   中英

Android save and show user preferences using custom DialogPreference

I'm trying to print the value of a editText in my preferenceActivity, and for that I'm using a custom DialogPreference so the user can enter the price to filter and when he clicks "ok" it should print the filtered price in the activity. But when I click "ok" it shows me nothing, what am I missing?

public class MyDialogShow extends DialogPreference {


    private View view;
    private EditText maximo;
    private EditText minimo;
    private TextView valores;
    private Context context;
    private View v;

    @Override
    protected View onCreateDialogView() {
        // TODO Auto-generated method stub
        view= super.onCreateDialogView();


            LayoutInflater inflater = LayoutInflater.from(getContext());

            v = inflater.inflate(R.layout.pref_valor, null);

            valores = (TextView) v.findViewById(R.id.escolhaValor);

        minimo = (EditText) view.findViewById(R.id.minimo);




        minimo.addTextChangedListener(new TextWatcher() {
            boolean isEdiging;
            private String current = "";

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                if (!s.toString().equals(current)) {
                    minimo.removeTextChangedListener(this);

                    String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
                    String cleanString = s.toString().replaceAll(replaceable, "");

                    double parsed;
                    try {
                        parsed = Double.parseDouble(cleanString);
                    } catch (NumberFormatException e) {
                        parsed = 0.00;
                    }
                    String formatted = NumberFormat.getCurrencyInstance().format((parsed/100));

                    current = formatted;
                    minimo.setText(formatted);
                    minimo.setSelection(formatted.length());
                    minimo.addTextChangedListener(this);
                }
            }
        });

        maximo = (EditText) view.findViewById(R.id.maximo);

        maximo.addTextChangedListener(new TextWatcher() {
            boolean isEdiging;
            private String current = "";

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!s.toString().equals(current)) {
                    maximo.removeTextChangedListener(this);

                    String replaceable = String.format("[%s,.\\s]", NumberFormat.getCurrencyInstance().getCurrency().getSymbol());
                    String cleanString = s.toString().replaceAll(replaceable, "");

                    double parsed;
                    try {
                        parsed = Double.parseDouble(cleanString);
                    } catch (NumberFormatException e) {
                        parsed = 0.00;
                    }
                    String formatted = NumberFormat.getCurrencyInstance().format((parsed / 100));

                    current = formatted;
                    maximo.setText(formatted);
                    maximo.setSelection(formatted.length());
                    maximo.addTextChangedListener(this);
                }
            }
        });

        return view;
    }






    @Override
    public void onClick(DialogInterface dialog, int which){


        if(which == DialogInterface.BUTTON_POSITIVE) {



            SecurePreferences mSessao = new SecurePreferences(getContext(), "sessao");
            mSessao.put("filtro_pedidos_valor", minimo.getText().toString());
            mSessao.put("filtro_pedidos_valor_maior", maximo.getText().toString());


            valores.setText(mSessao.getString("filtro_pedidos_valor") + " - " + mSessao.getString("filtro_pedidos_valor_maior"));


            System.out.println("VALOR >>> " + mSessao.getString("filtro_pedidos_valor") + " - " + mSessao.getString("filtro_pedidos_valor_maior"));


        }else if(which == DialogInterface.BUTTON_NEGATIVE){


            System.out.println("CANCEL BUTTON");

        }
    }



    public MyDialogShow(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
        super.setDialogLayoutResource(R.layout.dialog_layout);

        //super.setDialogIcon(R.drawable.ic);
    }

    public MyDialogShow(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        super.setDialogLayoutResource(R.layout.dialog_layout);

        //super.setDialogIcon(R.drawable.ic);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
        persistBoolean(positiveResult);
    }


}

pref_valor.xml:

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

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        android:gravity="center"
        android:paddingTop="10dp"
        android:paddingBottom="10dp">


        <TextView
            android:id="@+id/filtrarValor"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="Entre o valor"
            android:textColor="@color/black"
            android:textSize="@dimen/detalhe_cliente"
            android:entries="@array/listentries"
            android:entryValues="@array/listvalues"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="left" />

        <TextView
            android:id="@+id/escolhaValor"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text=""
            android:textColor="@color/blue_dark"
            android:textSize="@dimen/detalhe_cliente"
            android:layout_weight="1"
            android:padding="10dp"
            android:gravity="right" />





    </LinearLayout>



</LinearLayout>

pref_xml:

<PreferenceCategory android:title="@string/fValorTitulo">

    <br.com.representemais.MyDialogShow
        android:dialogTitle="@string/fValorTitulo"
        android:key="prefValor"
        android:layout="@layout/pref_valor"
        android:summary="Entre o valor"
        android:positiveButtonText="OK"
        android:negativeButtonText="Cancel" />

    </PreferenceCategory>

在此处输入图片说明

Declare current outside of the listener. Below the class declaration if you want to access it through out the class.

Also read up on scope: http://www.java-made-easy.com/variable-scope.html http://code.tutsplus.com/tutorials/learn-java-for-android-development-java-syntax--mobile-2612

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM