2012-03-16 9 views
27

私はCompositeComponent(EditText + ImageButton)を持っています ボタンをクリックすると、編集テキストの内容が消去されます。 正常に動作しています。私の問題は、私のコンポーネントに属性を設定することです。私はdeclare-styleableを使用してコンポーネントに属性を設定しています。カスタムコンポーネントの入力タイプを設定するためにdeclareを使用します

minLines、maxLines、textColorの設定に成功しました。

xmlを使用してinputtypeをコンポーネントに設定する方法を教えてください。

main_layout.xmlで

私attributes.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="CET"> 
     <attr name="MaxLines" format="integer"/> 
     <attr name="MinLines" format="integer"/> 
     <attr name="TextColor" format="color"/> 
     <attr name="InputType" format="integer" /> 
     <attr name="Hint" format="string" /> 
    </declare-styleable> 
</resources> 

そしてMyComponentのの使用:

<com.test.ui.ClearableEditText 
     xmlns:cet="http://schemas.android.com/apk/res/com.test.ui" 
     android:id="@+id/clearableEditText2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     cet:MaxLines="2" 
     cet:MinLines="1" 
     cet:TextColor="#0000FF" 
     cet:InputType="" <---I cant set this property---------> 
     cet:Hint="Clearable EditText Hint"> 

    </com.test.ui.ClearableEditText> 

通常のEditText用法:

<EditText 
     android:id="@+id/editText1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:inputType="numberSigned" <--------I want to use this property--------> > 

私は私の属性でENUMを使用傾けます。 xml。 cet:InputTypeandroid:inputType="numberSigned"を紹介する方法はありますか?

EDIT:これは私が私のClearableEditText.javaでプロパティを割り当てる方法を

TypedArray a = getContext().obtainStyledAttributes(attrs,R.styleable.CET,0, 0); 

      int minLines = a.getInt(R.styleable.CET_MinLines, 1); 
      int maxLines = a.getInt(R.styleable.CET_MaxLines, 100); 
      String hint = a.getString(R.styleable.CET_Hint); 
      int textColor = a.getColor(R.styleable.CET_TextColor, Color.BLACK); 
      int inputType = a.getInt(R.styleable.CET_InputType, -108); 

      Log.i(TAG, "ClearableEditText: Min Line "+minLines +" Max Lines: "+maxLines+" Hint "+hint+" Color: "+textColor+" Input Type: "+inputType); 

      edit_text.setMaxLines(maxLines); 
      edit_text.setMinLines(minLines); 
      edit_text.setTextColor(textColor); 
      edit_text.setHint(hint); 
      if(inputType != -108) 
       edit_text.setInputType(inputType); 

ある

あなたはEDITTEXTにinputTypeのプロパティを割り当てることで問題はありません見ることができます。

+0

を得るのですか?このリンクを使用してより多くのフラグを追加する – Mayank

+0

私は何のエラーも起こりませんでした。そのうまく動作します。私はどのように入力の種類を割り当てるか分からない?生の整数値を与える代わりに、アンドロイドでenum(android:inputtype)を使いたいと思います。 –

答えて

0

私は生のint値でそれを行うことができました: 私はそれが良い習慣ではないと思います。 私はこのような生の値に割り当てることができます(値については言及:http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBERhttp://developer.android.com/reference/android/R.styleable.html#TextView_inputType)数のcet:InputType="2"

2を

私は​​3210は助けることができるが、それを使用する方法がわからないと信じています。

59

あなたは、TextViewではないInputViewという名前のカスタムビューを持っていると言います(RelativeLayoutと言うことができます)。

... 

    private String title; 
    private int inputType; 
    private int imeOptions; 

    ... 
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.InputView); 

    int n = a.getIndexCount(); 
    for (int i = 0; i < n; i++) { 
     int attr = a.getIndex(i); 
     switch (attr) { 
     case R.styleable.InputView_title: 
      title = a.getString(attr); 
      break; 
     //note that you are accessing standart attributes using your attrs identifier 
     case R.styleable.InputView_android_inputType: 
      inputType = a.getInt(attr, EditorInfo.TYPE_TEXT_VARIATION_NORMAL); 
      break; 
     case R.styleable.InputView_android_imeOptions: 
      imeOptions = a.getInt(attr, 0); 
      break; 
     default: 
      Log.d("TAG", "Unknown attribute for " + getClass().toString() + ": " + attr); 
      break; 
     } 
    } 

    a.recycle(); 
    ... 
:あなたはいつものように属性を抽出することができますカスタムクラスの内部で

<!-- note xmlns:custom and com.mycompany.myapp --> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:custom="http://schemas.android.com/apk/res-auto" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

     <!-- note that you will be using android: prefix for standart attributes, and not custom: prefix --> 
     <!-- also note that you can use standart values: actionNext or textEmailAddress --> 
     <com.mycompany.myapp.InputView 
     android:id="@+id/emailField" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     custom:title="@string/my_title" 
     android:imeOptions="actionNext|flagNoExtractUi" 
     android:inputType="textEmailAddress" /> 

</FrameLayout> 

:あなたはInputViewを含めるXMLレイアウトで

<declare-styleable name="InputView"> 

    <!-- any custom attributes --> 
    <attr name="title" format="string" /> 

    <!-- standart attributes, note android: prefix and no format attribute --> 
    <attr name="android:imeOptions"/> 
    <attr name="android:inputType"/> 

</declare-styleable> 

:あなたのattrs.xmlで

+0

私はこれが非常に古い投稿だと知っていますが、それはedittextのように提案を表示する方法があるかどうか疑問に思っていましたか? – dzsonni

+0

私は日食ショーの提案をすることができませんでした。多分、将来のadtのアップデートでうまくいくでしょう。 – Alexey

+0

このコードは機能しますか?私はここに書かれているようにすべてを行ったので、カスタムクラスではinputType属性を取得できません。 case R.styleable.InputView_android_inputType: – Jilberta

2

あなたのattr.xmlでこれを使用してください。 edittextを使用している間、フラグを使用するとxmlの提案が得られます。 custom:inputType = "text" そしてそれはあなたに追加タイプを提案します。あなたはどのようなエラーを 訪問https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/attrs.xml

<resources> 

<declare-styleable name="ClearableEditText"> 
    <attr name="hintText" format="string" /> 
    <attr name="inputType" format="integer"> 

     <!-- There is no content type. The text is not editable. --> 
     <flag name="none" value="0x00000000" /> 
     <!-- 
Just plain old text. Corresponds to 
{@link android.text.InputType#TYPE_CLASS_TEXT} | 
{@link android.text.InputType#TYPE_TEXT_VARIATION_NORMAL}. 
     --> 
     <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}. 
     --> 
    </attr> 
    </declare-styleable> 

</resources> 
関連する問題