2014-09-01 152 views
1

我正在尝试使用custom:inputType="text"custom:inputType="phone"。 帮助我,edit_text总是设置defvalue。 但与索引值 一个问题,如果我使用format="integer"然后问题定义自定义:inputType for ClearableEditText

R.java

给出错误。

这里是main.xml

<com.example.ClearableEditText.ClearableEditText 
        android:id="@+id/name" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:ems="10" 
        custom:hintText="@string/name" 
        custom:inputType="text" /> 
</LinearLayout> 

attr.xml。凡我所使用的inputType的格式为整数

<declare-styleable name="ClearableEditText"> 
    <attr name="hintText" format="string" /> 
    <attr name="inputType" format="integer"> 
     <flag name="text" value="0x00000001" /> 
     <!-- 
     Can be combined with <var>text</var> and its variations to 
     request capitalization of all characters. Corresponds to 
     {@link android.text.InputType#TYPE_TEXT_FLAG_CAP_CHARACTERS}. 
     --> 
     <flag name="phone" value="0x00000003" /> 
     <!-- 
     For entering a date and time. Corresponds to 
     {@link android.text.InputType#TYPE_CLASS_DATETIME} | 
     {@link android.text.InputType#TYPE_DATETIME_VARIATION_NORMAL}. 
     --> 
    </attr> 
</declare-styleable> 

这是类ClearableEditText

void initViews(Context context, AttributeSet attrs) { 

    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, 
      R.styleable.ClearableEditText, 0, 0); 

    try { 
     hintText = a.getString(R.styleable.ClearableEditText_hintText); 
     inputType=a.getResourceId(R.styleable.ClearableEditText_inputType, android.text.InputType.TYPE_CLASS_TEXT); 
    } finally { 
     a.recycle(); 
     initViews(); 
    } 
} 

void initViews() { 
    inflater = (LayoutInflater) getContext().getSystemService(
      Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.second, this, true); 
    edit_text = (EditText) findViewById(R.id.clearable_edit); 
    btn_clear = (Button) findViewById(R.id.clearable_button_clear); 
    btn_clear.setVisibility(RelativeLayout.INVISIBLE); 
    edit_text.setHint(hintText); 
    edit_text.setInputType(inputType); 
    } 
+0

主要问题是,edit_text.setInputType(inputType);设置了默认的android.text.InputType.TYPE_CLASS_TEXT。 – 2014-09-01 05:27:22

回答

1

的代码,我FO和错误。 哈哈,我使用这个getResourceId(int,int); 但attr.xml我已经声明它为整数。

inputType=a.getResourceId(R.styleable.ClearableEditText_inputType,android.text.InputType.TYPE_CLASS_TEXT); 

但把它作为一个整数修复每一个。

inputType=a.getInteger(R.styleable.ClearableEditText_inputType,android.text.InputType.TYPE_CLASS_TEXT);