0

我试图在我的preferenceActivity中打印editText的值,为此我使用自定义DialogPreference,以便用户可以输入要过滤的价格以及何时点击“确定”它应该在活动中打印过滤后的价格。但是当我点击“确定”它什么都没有显示,我错过了什么?Android保存并使用自定义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> 

enter image description here

回答

-1
+0

我已经更新的问题,并调试它,我得到一个空LayoutInflater,原因是:

的范围还读了? – jgandroid 2014-10-28 17:46:50

+0

请阅读文档。这是学习的最佳方式! http://developer.android.com/reference/android/view/LayoutInflater.html – 2014-10-28 17:50:52

相关问题