繁体   English   中英

Android使用自定义DialogPreference保存并显示用户首选项

[英]Android save and show user preferences using custom DialogPreference

我正在尝试在我的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>

在此处输入图片说明

在侦听器外部声明电流。 如果要在整个类中访问它,请在类声明下面。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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