だから私はAndroid 4.4でカスタムスタイル可能に問題があります。
Android 4.4 で何が問題なのか、何が余分なのかを誰かが知っていれば、カスタム スタイル可能オブジェクトが起動しません。助けてください。
これが私が使用するコードで、Android 2.2から4.3までで完全に動作します(電話とシミュレーターでテスト済み)。
レイアウト :
<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) {
}
}