2012-03-16 18 views
27

स्थापित करने के लिए मैं एक CompositeComponent (EditText + ImageButton) जब edittext सामग्री बटन पर क्लिक साफ कर दिया जाएगा है Styleable घोषणा करते हैं। यह ठीक काम कर रहा है। मेरी समस्या मेरे घटक को विशेषताओं को सेट कर रही है। मैं अपने घटक में विशेषताओं को सेट करने के लिए घोषणा-शैली योग्य उपयोग कर रहा हूं।उपयोग कस्टम घटक इनपुट प्रकार

मैं स्थापित करने minLines, Maxlines और textcolor में सफल रहा हूँ।

मैं कैसे एक्सएमएल के माध्यम से मेरी घटक को inputtype सेट कर सकते हैं।

मेरी 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 के उपयोग main_layout.xml में:

<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 का उपयोग नहीं कर सकते। एक्सएमएल। android:inputType="numberSigned" को मेरे cet:InputType में कैसे देखें?

संपादित करें:

इस तरह मैं अपने 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 मान के साथ ऐसा करने में कामयाब रहा: मुझे लगता है कि यह अच्छा अभ्यास नहीं है। मैं इस तरह कच्चे मान निर्दिष्ट कर सकते हैं: cet:InputType="2"

2 नंबर के लिए (मूल्यों के लिए भेजा: http://developer.android.com/reference/android/text/InputType.html#TYPE_CLASS_NUMBER और http://developer.android.com/reference/android/R.styleable.html#TextView_inputType)

मेरा मानना ​​है कि <attr name="InputType" format="reference" /> मदद कर सकते हैं, लेकिन यह कैसे उपयोग करने के लिए पता नहीं है।

59

आइए कहें कि आपके पास इनपुट व्यू नामक एक कस्टम दृश्य है, जो टेक्स्टव्यू नहीं है (इसे एक सापेक्ष लेटआउट कहें)।

अपने attrs.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> 

एक XML लेआउट जहां InputView शामिल करना चाहते हैं: अपने कस्टम वर्ग के अंदर

<!-- 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> 

आप हमेशा की तरह विशेषताओं निकाल सकते हैं:

... 

    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(); 
    ... 
+0

मुझे पता है कि यह एक बहुत पुरानी पोस्ट है, लेकिन मैं सोच रहा था कि ग्रहण करने के लिए कोई तरीका है जैसे कि संपादन के लिए सुझाव देता है? – dzsonni

+0

मैं ग्रहण शो सुझाव बनाने में सक्षम नहीं था। शायद यह एडीटी के भविष्य के अपडेट में काम करेगा। – Alexey

+0

क्या यह कोड काम करता है? क्योंकि मैंने यहां लिखा है सब कुछ किया है, लेकिन मेरी कस्टम कक्षा में मुझे इनपुट टाइप नहीं मिल सकता है। इस लाइन को हल नहीं कर सकता: केस R.styleable.InputView_android_input टाइप: – Jilberta

2

सुझावों के लिए अपने attr.xml में इस प्रयोग। झंडे का उपयोग edittext का उपयोग करते समय xml में सुझाव देते हैं। कस्टम: इनपुट टाइप = "टेक्स्ट" और यह आपको अतिरिक्त प्रकार का सुझाव देगा। जोड़ने के लिए अधिक झंडे के लिए इस लिंक पर जाएँ 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> 
संबंधित मुद्दे