android的EditText控件,内容右对齐
EditText光标在右边,输入内容从右往左:
android:gravity="center_vertical|right"
或者
editText.setGravity(Gravity.RIGHT);
设置只能输入整数,如果是小数则是:decimal
editText.setError(CharSequence error); 显示错误提示
etUserName.setTransformationMethod(new EditTextBgToStar());
public class EditTextBgToStar extends PasswordTransformationMethod {
@Override
public CharSequence getTransformation(CharSequence source, View view) {
return new PasswordCharSequence(source);
}
private class PasswordCharSequence implements CharSequence {
private CharSequence mSource;
public PasswordCharSequence(CharSequence source) {
mSource = source; // Store char sequence
}
public char charAt(int index) {
return '*'; // This is the important part
}
public int length() {
return mSource.length(); // Return default
}
public CharSequence subSequence(int start, int end) {
return mSource.subSequence(start, end); // Return default
}
}
}
改变密码样式
public class Typefaces {
private static final String TAG = "Typefaces";
private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();
public static Typeface get(Context c, String assetPath) {
synchronized (cache) {
if (!cache.containsKey(assetPath)) {
try {
Typeface t = Typeface.createFromAsset(c.getAssets(), assetPath);
cache.put(assetPath, t);
} catch (Exception e) {
Log.e(TAG, "Could not get typeface '" + assetPath + "' because " + e.getMessage());
return null;
}
}
return cache.get(assetPath);
}
}
}
// set fancy typeface
tv.setTypeface(Typefaces.get(this, "Satisfy-Regular.ttf")); // <pre name="code" class="java">Satisfy-Regular.ttf" 在assets中
//像素转换
public float dp2px(float dp) {
final float scale = getResources().getDisplayMetrics().density;
return dp * scale + 0.5f;
}
public float sp2px(float sp) {
final float scale = getResources().getDisplayMetrics().scaledDensity;
return sp * scale;
}