android的EditText控件技巧

来源:互联网 发布:网络表情包原型 编辑:程序博客网 时间:2024/06/16 12:24

android的EditText控件,内容右对齐

 

EditText光标在右边,输入内容从右往左:
android:gravity="center_vertical|right"
或者
editText.setGravity(Gravity.RIGHT);

 

 

 

android:numeric="integer"

设置只能输入整数,如果是小数则是:decimal


editText.setError(CharSequence error); 显示错误提示


etUserName.setTransformationMethod(new EditTextBgToStar());public class EditTextBgToStar extends PasswordTransformationMethod {@Overridepublic 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 typefacetv.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;}




0 0