2017-04-17 1 views
0

내가이 분홍색 상자가 있다고 가정 : 필드 이름으로 TextViewEditText :EditText의 부모를 클릭 할 수있게 만드는 방법?

pinku bokkusu

그것은 그것의 아이들과 함께 LinearLayout로 구성되어 있습니다. EditText은 의도적으로 사용 중지되었습니다. 내가 원하는 것은 사용자가 분홍색 상자에서 원하는 곳을 클릭 할 수 있다는 것입니다. 그건 그렇고, 당신이 이상한 것을 발견 한 UI/UX를 무시하십시오.

시도했지만 사용자가 EditText이 차지하는 영역을 탭할 수 없습니다. 사용자는 TextView 또는 분홍색 상자의 빈 영역을 탭해야 앱에 '클릭'이 표시됩니다. 그러나 사용자가 EditText의 영역을 탭하면 아무 일도 일어나지 않습니다.

나는 그런 설정 LinearLayout 같은 XML의 특성 몇 가지, 함께 연주 해봤의 clickabletrue, 그리고 모든 어린이하거나 EditText '아무 소용이 false-clickable, focusablefocusableInTouchMode, 모두의 속성. 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를 사용하는 경우

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

를 (1.8 기능을 사용) 람다를 사용할 수있다 24보다 짧게 만들 수도 있습니다 :

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

리스너를 클릭하는 대신 터치 리스너를 추가하기 만하면됩니다.

관련 문제