0

我正在尝试在我的preferenceActivity中打印editText的值,为此我正在使用自定义DialogPreference,因此用户可以输入要过滤的价格,当他单击“确定”时,它应该在活动中打印过滤后的价格. 但是当我单击“确定”时,它什么也没显示,我错过了什么?

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>

在此处输入图像描述

4

1 回答 1

-1

在侦听器之外声明当前。如果您想通过类访问它,请在类声明下方。

还阅读范围: http ://www.java-made-easy.com/variable-scope.html http://code.tutsplus.com/tutorials/learn-java-for-android-development-java-syntax --mobile-2612

于 2014-10-28T17:27:09.720 回答