2017-04-17 35 views
0

假設我有這樣的粉紅色框:如何使EditText的父級可點擊?

pinku bokkusu

它由LinearLayout與其子:TextView的字段名和EditTextEditText被故意禁用。我想要的是,用戶可以在用戶想要的粉紅色框上點擊任何地方。順便說一句,請忽略任何您發現奇怪的UI/UX事物。

我試過,但用戶無法發掘這一EditText佔地面積。用戶必須點擊TextView或粉紅色框上的空白區域,以便應用程序獲得「點擊」。但是如果用戶點擊EditText的地區,什麼都不會發生。

我試着用XML的性能有一定的東西,比如設置LinearLayout打的clickabletrue,和所有的孩子或只是EditText'的clickablefocusablefocusableInTouchModefalse,一切都是徒勞的屬性。 EditText區域仍然無法點擊。

有什麼想法?難道只能通過xml達到嗎?應該以編程方式完成,只是爲了繞過EditText的點擊?

+0

你的描述很混亂。 – RATHI

+0

發佈edittext的佈局代碼 – rafsanahmad007

回答

0

您需要通過的意見,要求父佈局(LinearLayout中,不管)和循環,如果你不想將它們綁定所有。如果你使用數據綁定更容易。無論如何,這裏是一個解決方案(一小段代碼是需要!)。

佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical"> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="out of focus"/> 

    <LinearLayout 
     android:id="@+id/linearTest" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     > 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="test for clicking" 
      /> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="another clicking test" 
      /> 

     <EditText 
      android:id="@+id/editTest" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="focus edittext when clicking linearlayout or other elements inside" 
      /> 
    </LinearLayout> 

</LinearLayout> 

代碼:

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 


     setContentView(R.layout.linear_focus_test); 

     final EditText editText = (EditText) findViewById(R.id.editTest); 

     LinearLayout linearTest = (LinearLayout) findViewById(R.id.linearTest); 
      for (int i = 0; i < linearTest.getChildCount(); i++) 

      View v = linearTest.getChildAt(i); 
      v.setOnClickListener(new View.OnClickListener() { 
       @Override public void onClick(View v) { 
        editText.requestFocus(); 
       } 
      }); 
     } 
} 

如果你不喜歡的樣板,你也可以使用拉姆達如果您至少使用API​​(使用1.8功能)

for (int i = 0; i < linearTest.getChildCount(); i++) 
      linearTest.getChildAt(i).setOnClickListener(v1 -> editText.requestFocus()); 

24你甚至可以縮短:

IntStream.range(0, linearTest.getChildCount()).forEach(i -> linearTest.getChildAt(i).setOnClickListener(v1 -> editText.requestFocus())); 
1

您可以簡單地添加onTouch監聽器,而不是點擊監聽器。