2016-01-08 7 views
6

के बायीं ओर खींचने वाले ऊपर से एंकर के लिए TextInputLayout लेबल, क्या TextInputLayout लेबल को संपादन टेक्स्ट में बाएं ड्रॉ करने योग्य के ऊपर दिखाने के लिए संभव है जब उपयोगकर्ता संपादन टेक्स्ट में केंद्रित या टाइप करता है।एडिटटेक्स्ट

enter image description here

:

enter image description here

यहाँ उत्पादन है कि मैं हो रही है:

<android.support.design.widget.TextInputLayout 
       android:id="@+id/completion_date_layout2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:layout_marginLeft="5dp" 
       android:layout_marginRight="5dp" 
       android:layout_weight="1" 
       android:orientation="horizontal"> 

       <EditText 
        android:id="@+id/etTaskDate" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:hint="@string/title_completion_date" 
        android:inputType="text" 
        android:drawableLeft="@drawable/ic_date" 
        android:paddingBottom="15dp" 
        android:drawablePadding="5dp" 
        android:textSize="@dimen/fields_text_size"/> 
      </android.support.design.widget.TextInputLayout> 

यहाँ वांछित आउटपुट है:

यहाँ EditText की एक्सएमएल है

+2

क्या आपको कभी समाधान मिला? मुझे एक ही समस्या है। –

+0

@RaymonddelaCroix दुर्भाग्य से पिछले महीने से मेरे दोस्त नहीं। मैंने जो काम किया है उसके नीचे मैंने पोस्ट किया है। :) – icaneatclouds

उत्तर

3

जावा के सदस्य पहुँच मॉडल और गूगल के डेवलपर्स जो एक छोटे से बचाव यह एक साधारण उपवर्गीकरण जो मूल कोड की एक न्यूनतम दोहराता साथ प्राप्त किया जा सकता छोड़ दिया करने के लिए धन्यवाद एक ही पैकेज जो पैकेज-स्तरीय दृश्यता के साथ mCollapsingTextHelper तक पहुंच खोलता है और उसके बाद मूल नाम onLayout विधि से कोड का हिस्सा दोहराता है जो फ़ील्ड नाम स्थिति प्रबंधित करता है। __4DP__ मान 4 डीपी मान पिक्सेल में कनवर्ट किया गया है, मुझे पूरा यकीन है कि इसके लिए सभी के पास उपयोगिता विधि है।

अपने XML लेआउट में सिर्फ android.support.design.widget.TextInputLayoutandroid.support.design.widget.TextInputLayoutEx करने से स्विच तो अपने लेआउट इस तरह दिखता है:

<android.support.design.widget.TextInputLayoutEx 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Mobile"> 
    <android.support.design.widget.TextInputEditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:drawableLeft="@drawable/ic_phone_black_24dp" 
     android:drawablePadding="4dp"/> 
</android.support.design.widget.TextInputLayoutEx> 

और परिणाम

Collapsed and expanded state

है क्षण यह com.android.support:design:25.3.1

लिए काम करता है पर
+0

इसे किसी अन्य प्रोजेक्ट पर आज़माएगा। धन्यवाद – icaneatclouds

+0

ईपीआईसी, एक आकर्षण की तरह काम किया। @icaneatclouds इस कोड को 'उत्तर' के रूप में चिह्नित करने पर विचार करते हैं। – MiguelCatalan

-1

मैंने एक कामकाज किया है इस मुद्दे के बारे में डी। यह थोड़ा अविश्वसनीय हो सकता है लेकिन यह मेरे अंत में काम करता है।

String spacer="  "; 
EditText myEditText= (EditText)findViewById(R.id.myEditText); 
myEditText.setText(spacer + "Today"); 
myEditText.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_calendar, 0, 0, 0); 

क्या मैं यहाँ editText के अंदर का पाठ करने से पहले एक स्पेसर रखकर किया गया था था, तो जोड़ने drawable प्रोग्राम के लिए छोड़ दिया: यहाँ कोड है।

लेकिन editText की सामग्रियों को लाते समय से पहले उन रिक्त स्थान को हटाना न भूलें:

String title = myEditText.getText().toString().substring(8); 

इसका मतलब यह है मैं शब्द "आज" से पहले 8 रिक्त स्थान काटी।

+0

क्या आप डाउनवोट समझा सकते हैं? मेरे प्रश्न पर कोई वैध जवाब नहीं है इसलिए मैंने त्वरित कार्यवाही की। – icaneatclouds

2

आप बाएं आइकन को एनिमेट करने के लिए एनीमेशन और फ्रेम_लेआउट का उपयोग कर सकते हैं, this लिंक आज़माएं, आपके लिए सहायक हो सकता है।

package android.support.design.widget; 

import android.content.Context; 
import android.graphics.Rect; 
import android.util.AttributeSet; 

public final class TextInputLayoutEx extends TextInputLayout { 

    private final int mDefaultPadding = __4DP__; 
    private final Rect mTmpRect = new Rect(); 

    public TextInputLayoutEx(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    @Override 
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 
     super.onLayout(changed, left, top, right, bottom); 
     if (isHintEnabled() && mEditText != null) { 
      final Rect rect = mTmpRect; 
      ViewGroupUtils.getDescendantRect(this, mEditText, rect); 
      mCollapsingTextHelper.setCollapsedBounds(
       rect.left + mDefaultPadding, getPaddingTop(), 
       rect.right - mDefaultPadding, bottom - top - getPaddingBottom()); 
      mCollapsingTextHelper.recalculate(); 
     } 
    } 

} 

यहाँ हम करने के लिए एक नया वर्ग डाल:

संबंधित मुद्दे