Android olIndex

Android olIndex,android,android-view,android-styles,declare-styleable,Android,Android View,Android Styles,Declare Styleable,olIndex

olIndex<0){ mEditTextValue.setSelection(格式化的.length()); }否则{ mEditTextValue.setSelection(符号索引-1); } mEditTextValue.addTextChangedListener(此); } int getNumberOfDecimalPlace(字符串值){ 字符串=条带符号(值); DecimalFormat=(DecimalFormat)NumberFormat.getInstance(); DecimalFormatSymbols symbols=format.getDecimalFormatSymbols(); final int index=string.lastIndexOf(symbols.getDecimalSeparator()); 如果(指数<0){ 返回0; } 返回字符串.length()-1-索引; } 公共浮点getFloatValue(){ 浮点数=0; String String=mEditTextValue.getEditableText().toString(); 字符串cleanString=cleanString(字符串); 如果(!TextUtils.isEmpty(cleanString) &&!(cleanString.length()==1&&!TextUtils .isDigitsOnly(cleanString))){ value=Float.valueOf(cleanString); //int exp=getNumberOfDecimalPlace(字符串); 数值/=数学功率(10,2); } 返回值; } 公共无效设置浮动值(浮动值){ DecimalFormat nf=(DecimalFormat)NumberFormat.getInstance(); nf.setNegativePrefix(“”); nf.setNegativeSuffix(“)”; nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); 格式化字符串=nf.format(值); mEditTextValue.removeTextChangedListener(此); this.mEditTextValue.setText(格式化); mEditTextValue.setSelection(格式化的.length()); mEditTextValue.addTextChangedListener(此); } public void setFloatValueWithPercentage(浮动值){ DecimalFormat nf=(DecimalFormat)NumberFormat.getInstance(); nf.setNegativePrefix(“”); nf.setNegativeSuffix(“)”; nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); 格式化字符串=nf.format(值); 格式化+=“%”; mEditTextValue.removeTextChangedListener(此); this.mEditTextValue.setText(格式化); 字符串符号=“%”; int-symbolIndex=格式化的.indexOf(符号); 如果(SYMBOLNDEX==0 | | SYMBOLNDEX<0){ mEditTextValue.setSelection(格式化的.length()); }否则{ mEditTextValue.setSelection(符号索引-1); } mEditTextValue.addTextChangedListener(此); } public void setFloatValueWithCurrency(浮动值){ DecimalFormat nf=(DecimalFormat)NumberFormat.getCurrencyInstance(); nf.setNegativePrefix(“”); nf.setNegativeSuffix(“)”; nf.setMaximumFractionDigits(2); nf.setMinimumFractionDigits(2); 格式化字符串=nf.format(值); mEditTextValue.removeTextChangedListener(此); this.mEditTextValue.setText(格式化); String symbol=nf.getCurrency().getSymbol(); int-symbolIndex=格式化的.indexOf(符号); 如果(SYMBOLNDEX==0 | | SYMBOLNDEX<0){ mEditTextValue.setSelection(格式化的.length()); }否则{ mEditTextValue.setSelection(符号索引-1); } mEditTextValue.addTextChangedListener(此); } 字符串条带符号(字符串){ if(字符串==null) 返回“”; 字符串可替换=“[” +NumberFormat.getCurrencyInstance().getCurrency() .getSymbol()+“%\\(\\)\\s]”; String cleanString=String.replaceAll(可替换“”); 返回干净字符串; } 字符串清理字符串(字符串){ if(字符串==null) 返回“”; 字符串可替换=“[” +NumberFormat.getCurrencyInstance().getCurrency() .getSymbol()+“%,.\\s]”; String cleanString=String.replaceAll(可替换“”); if(cleanString.contains(“”){ cleanString.replaceAll(“\\(\\)”,”); cleanString=“-”+cleanString; } 返回干净字符串; } @凌驾 公共无效后文本已更改(可编辑){ notifyCellUpdateListener(); } @凌驾 更改前的公共无效(字符序列、整数开始、整数计数、, 整数后){ } @凌驾 public void onTextChanged(字符序列、int start、int before、int count){ } }
     <com.example.mobile.view.CustomCell
                xmlns:cc="http://schemas.android.com/apk/res/com.example.mobile"
                android:id="@+id/B1"
                android:layout_width="fill_parent"
                android:layout_height="@dimen/customcell_height"
                android:inputType="number|numberDecimal"
                cc:editable="false"
                cc:labelText="@string/GrossIncome" />
package com.example.mobile.view;
public class CustomCell extends RelativeLayout implements TextWatcher {

private Context mContext;
private AttributeSet mAttributeSet;

private TextView mTextViewLabel;
private EditText mEditTextValue;

private boolean mEditable = false;

private CellUpdateListener mCellUpdateListener;

public CustomCell(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

    mContext = context;
    mAttributeSet = attributeSet;

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

    mTextViewLabel = (TextView) findViewById(R.id.customcell_label);
    mEditTextValue = (EditText) findViewById(R.id.customcell_value);

    Typeface tf = Typeface.createFromAsset(context.getAssets(),
                    "fonts/Calibri.ttf");
    mTextViewLabel.setTypeface(tf);
    mEditTextValue.setTypeface(tf);
    parseAttributes();
}

private void parseAttributes() {
    String text = null;
    TypedArray a = mContext.obtainStyledAttributes(mAttributeSet,
                    R.styleable.com_example_mobile_view_CustomCell);
    CharSequence s = a
                    .getString(R.styleable.com_example_mobile_view_CustomCell_labelText);
    if (s != null) {
        text = s.toString();
        setLabelText(text);
    }

    s = a.getString(R.styleable.com_example_mobile_view_CustomCell_valueText);
    if (s != null) {
        text = s.toString();
        setTextValue(text);
    }

    boolean b = a.getBoolean(
                    R.styleable.com_example_mobile_view_CustomCell_editable,
                    true);
    setEditable(b);

    b = a.getBoolean(
                    R.styleable.com_example_mobile_view_CustomCell_highlight,
                    false);
    if (b) {
        Drawable drawable = this.getResources().getDrawable(
                        R.drawable.input_highlighted);

        findViewById(R.id.customcell).setBackgroundDrawable(drawable);
        findViewById(R.id.customcell).setPadding(0, 0, 0, 0);
    }

    mEditTextValue.setInputType(a
                    .getInt(R.styleable.com_example_mobile_view_CustomCell_android_inputType,
                                    InputType.TYPE_CLASS_TEXT));

    a.recycle();
}

public void setCellUpdateListener(CellUpdateListener cellUpdateListener) {
    mCellUpdateListener = cellUpdateListener;

}

public void setOnFocusChangeListener(OnFocusChangeListener l) {
    mEditTextValue.setOnFocusChangeListener(l);
    mEditTextValue.setInputType(InputType.TYPE_NULL);
}

private void notifyCellUpdateListener() {
    if (mCellUpdateListener != null) {
        mCellUpdateListener.onCellUpdate(this);
    }
}

public void setEditable(boolean editable) {
    mEditable = editable;
    mEditTextValue.setEnabled(editable);
    mEditTextValue.setFocusableInTouchMode(editable);
    mEditTextValue.setFocusable(editable);
    if (mEditable) {
        mEditTextValue.setBackgroundResource(R.drawable.border);
        mEditTextValue.addTextChangedListener(this);
    } else {
        mEditTextValue.removeTextChangedListener(this);
    }
}

public String getLabelText() {
    return (String) mTextViewLabel.getText();
}

public void setLabelText(String label) {
    this.mTextViewLabel.setText(label);
}

public String getTextValue() {
    return mEditTextValue.getEditableText().toString();
}

public void setTextValue(String value) {
    mEditTextValue.removeTextChangedListener(this);
    mEditTextValue.setText(value);
    mEditTextValue.addTextChangedListener(this);
}

public int getIntValue() {
    int value = 0;
    String string = mEditTextValue.getEditableText().toString();
    String cleanString = cleanString(string);

    if (!TextUtils.isEmpty(cleanString)
                    && !(cleanString.length() == 1 && !TextUtils
                                    .isDigitsOnly(cleanString))) {
        value = Integer.valueOf(cleanString);
    }

    return value;
}

public void setIntValue(int value) {
    DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance();
    nf.setNegativePrefix("(");
    nf.setNegativeSuffix(")");
    nf.setMaximumFractionDigits(0);

    String formated = nf.format(value);

    mEditTextValue.removeTextChangedListener(this);
    this.mEditTextValue.setText(formated);
    mEditTextValue.setSelection(formated.length());
    mEditTextValue.addTextChangedListener(this);
}

public void setIntValueWithPercentage(int value) {
    DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance();
    nf.setNegativePrefix("(");
    nf.setNegativeSuffix(")");
    nf.setMaximumFractionDigits(0);

    String formated = nf.format(value);
    formated += " %";

    mEditTextValue.removeTextChangedListener(this);
    this.mEditTextValue.setText(formated);

    String symbol = "%";

    int symbolIndex = formated.indexOf(symbol);
    if (symbolIndex == 0 || symbolIndex < 0) {
        mEditTextValue.setSelection(formated.length());
    } else {
        mEditTextValue.setSelection(symbolIndex - 1);
    }
    mEditTextValue.addTextChangedListener(this);
}

public void setIntValueWithCurrency(int value) {
    DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance();
    nf.setNegativePrefix("(");
    nf.setNegativeSuffix(")");
    nf.setMaximumFractionDigits(0);

    String formated = nf.format(value);

    mEditTextValue.removeTextChangedListener(this);
    this.mEditTextValue.setText(formated);

    String symbol = nf.getCurrency().getSymbol();

    int symbolIndex = formated.indexOf(symbol);
    if (symbolIndex == 0 || symbolIndex < 0) {
        mEditTextValue.setSelection(formated.length());
    } else {
        mEditTextValue.setSelection(symbolIndex - 1);
    }
    mEditTextValue.addTextChangedListener(this);
}

int getNumberOfDecimalPlace(String value) {
    String string = stripSymbols(value);
    DecimalFormat format = (DecimalFormat) NumberFormat.getInstance();
    DecimalFormatSymbols symbols = format.getDecimalFormatSymbols();

    final int index = string.lastIndexOf(symbols.getDecimalSeparator());
    if (index < 0) {
        return 0;
    }
    return string.length() - 1 - index;
}

public float getFloatValue() {
    float value = 0;
    String string = mEditTextValue.getEditableText().toString();
    String cleanString = cleanString(string);

    if (!TextUtils.isEmpty(cleanString)
                    && !(cleanString.length() == 1 && !TextUtils
                                    .isDigitsOnly(cleanString))) {
        value = Float.valueOf(cleanString);

        // int exp = getNumberOfDecimalPlace(string);

        value /= Math.pow(10, 2);
    }

    return value;
}

public void setFloatValue(float value) {
    DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance();
    nf.setNegativePrefix("(");
    nf.setNegativeSuffix(")");
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);

    String formated = nf.format(value);

    mEditTextValue.removeTextChangedListener(this);
    this.mEditTextValue.setText(formated);
    mEditTextValue.setSelection(formated.length());
    mEditTextValue.addTextChangedListener(this);
}

public void setFloatValueWithPercentage(float value) {
    DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance();
    nf.setNegativePrefix("(");
    nf.setNegativeSuffix(")");
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);

    String formated = nf.format(value);
    formated += " %";

    mEditTextValue.removeTextChangedListener(this);
    this.mEditTextValue.setText(formated);

    String symbol = "%";

    int symbolIndex = formated.indexOf(symbol);
    if (symbolIndex == 0 || symbolIndex < 0) {
        mEditTextValue.setSelection(formated.length());
    } else {
        mEditTextValue.setSelection(symbolIndex - 1);
    }
    mEditTextValue.addTextChangedListener(this);
}

public void setFloatValueWithCurrency(float value) {
    DecimalFormat nf = (DecimalFormat) NumberFormat.getCurrencyInstance();
    nf.setNegativePrefix("(");
    nf.setNegativeSuffix(")");
    nf.setMaximumFractionDigits(2);
    nf.setMinimumFractionDigits(2);

    String formated = nf.format(value);

    mEditTextValue.removeTextChangedListener(this);
    this.mEditTextValue.setText(formated);

    String symbol = nf.getCurrency().getSymbol();

    int symbolIndex = formated.indexOf(symbol);
    if (symbolIndex == 0 || symbolIndex < 0) {
        mEditTextValue.setSelection(formated.length());
    } else {
        mEditTextValue.setSelection(symbolIndex - 1);
    }
    mEditTextValue.addTextChangedListener(this);
}

String stripSymbols(String string) {
    if (string == null)
        return "";

    String replaceable = "["
                    + NumberFormat.getCurrencyInstance().getCurrency()
                                    .getSymbol() + "%\\(\\)\\s]";

    String cleanString = string.replaceAll(replaceable, "");

    return cleanString;
}

String cleanString(String string) {
    if (string == null)
        return "";

    String replaceable = "["
                    + NumberFormat.getCurrencyInstance().getCurrency()
                                    .getSymbol() + "%,.\\s]";

    String cleanString = string.replaceAll(replaceable, "");

    if (cleanString.contains("(")) {
        cleanString.replaceAll("\\(\\)", "");
        cleanString = "-" + cleanString;
    }

    return cleanString;
}

@Override
public void afterTextChanged(Editable s) {
    notifyCellUpdateListener();
}

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

}

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

}

}