2017-06-09 36 views
0

我需要觸摸toolTip彈出來繪製圖標的EditText有沒有辦法如何從EditText中的`drawableRight`圖標查看?

還有就是我的EditText與繪製圖標

enter image description here

當用戶點擊問號工具提示彈出應該是開放的,我需要像這樣

enter image description here

但這當前實現我做過沒有直接的方法。我在右側放置了不可見的視圖,並跟蹤點擊可繪製的圖標,當點擊圖標時,我在不可見的視圖上設置了工具提示彈出...所以這是不正確的方式,因爲我認爲。

但我不知道如何獲得這個可繪製的問號圖標作爲視圖?

我在做什麼錯?

填寫免費問

感謝

回答

0

試試這個:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     /> 

    <ImageView 
     android:id="@+id/questionMark" 
     android:layout_width="24dp" 
     android:layout_height="24dp" 
     android:layout_alignParentRight="true" 
     android:layout_centerVertical="true" 
     /> 

</RelativeLayout> 

然後你就可以在ImageView的有onClickListener。

希望這會有所幫助。

+0

不完全是,我已經嘗試過這種方法,但是當我需要從改變佈局的方向出現問題,左到右右到左,因爲我在應用程序中英文使用兩種語言和希伯來語...所以,如果你可以做同樣的事情,但與LinnearLayout它會好起來 –

+0

試試這個https://android-developers.googleblog.com/2013/03/native-rtl-support-in- android-42.html 基本上,這是在您將語言環境更改爲RTL語言時的佈局鏡像。 –

0

與您使用TextInputLayoutEditText,所以你必須使用https://github.com/ViHtarb/Tooltip庫的工具提示,並做一兩件事中的EditText的右邊添加您的問號(?)可繪製並應用下面的代碼爲的onClick該繪製:

editText.setOnTouchListener(new OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      final int DRAWABLE_LEFT = 0; 
      final int DRAWABLE_TOP = 1; 
      final int DRAWABLE_RIGHT = 2; 
      final int DRAWABLE_BOTTOM = 3; 

      if(event.getAction() == MotionEvent.ACTION_UP) { 
       if(event.getRawX() >= (editText.getRight() - editText.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 
        // Run your tool-tip action from here 
        Tooltip tooltip = new Tooltip.Builder(v) 
    .setText("Hello tooltip") 
    .show(); 

       return true; 
       } 
      } 
      return false; 
     } 
    }); 
+0

我沒有使用自定義,我使用這個庫https://github.com/ViHtarb/Tooltip,但無論如何與您的答案中的庫,我需要在XML中設置toolTip,但如何使用'TextInputLayout'呢?因爲我在'TextInputLayout'裏面使用我的'EditText' ... –

+0

我已經更新了代碼中的一些更改。 –

+0

是的,這是一個很好的解決方案,但仍然不夠好,因爲您的工具提示將出現在EditText視圖的中間,因爲您嘗試傳遞'v',但'v'它不是可繪製圖標'問題mark','v' - 在你的情況下是當前的'EditText' ...你可以嘗試自己檢查它。正因爲如此,我的問題如何獲得可繪製的「問號」圖標作爲視圖?我需要將此視圖傳遞給toolTip構建器... –

0

我做了一些欺騙。這是醜陋的,但爲我工作。您可以使用一個按鈕(作爲佔位符)與0dp高度和。庫使用anchorview僅查找彈出窗口的位置。 看看我下面如何。

這是XML

 <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

      <android.support.design.widget.TextInputLayout 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       app:errorEnabled="true" 
       app:hintTextAppearance="@style/TextInput"> 

       <android.support.design.widget.TextInputEditText 
        android:id="@+id/et_pn" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="@string/username_pn" 
        android:inputType="number" 
        android:maxLength="18" 
        android:drawableEnd="@drawable/ic_close" 
        app:theme="@style/EditText"/> 

      </android.support.design.widget.TextInputLayout> 

      <Button 
       android:id="@+id/placeholder" 
       android:layout_width="0dp" 
       android:layout_height="0dp" 
       android:layout_alignParentEnd="true" 
       android:layout_marginEnd="15dp" 
       android:layout_centerVertical="true"/> 

     </RelativeLayout> 

這是怎麼點擊按鈕繪製的應用。

TextInputEditText button; 
    private Button placeholder; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     button = (TextInputEditText) findViewById(R.id.et_pn); 
     placeholder = (Button) findViewById(R.id.placeholder); 

     button.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       final int DRAWABLE_LEFT = 0; 
       final int DRAWABLE_TOP = 1; 
       final int DRAWABLE_RIGHT = 2; 
       final int DRAWABLE_BOTTOM = 3; 

       if (event.getAction() == MotionEvent.ACTION_UP) { 
        if (event.getRawX() >= (button.getRight() - button.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) { 

         Tooltip.Builder builder = new Tooltip.Builder(MainActivity.this, placeholder) 
           .setCancelable(true) 
           .setDismissOnClick(true) 
           .setCornerRadius(20f) 
           .setGravity(Gravity.BOTTOM) 
           .setText(R.string.tooltip_hello_world); 
         builder.show(); 
         return false; 
        } 
       } 
       return false; 
      } 
     }); 

    } 
+0

正如我之前告訴過的@sarthak Gandhi:不完全一樣,我已經嘗試了這種方法,但是當我需要將佈局方向從左到右更改爲從右到左,因爲我在應用程序中使用了兩種語言英語和希伯來語......所以,如果您可以做同樣的事情,但是使用LinnearLayout,它會很好 –

相關問題