2014-10-03 5 views
1

설정 화면에서 작업 중이며 Android의 환경 설정 API를 사용하여 인터페이스를 작성하고 있습니다. 환경 설정 중 하나는 버튼입니다. 환경 설정에 맞춤 레이아웃을 추가하여 작업을 완료했습니다. 그것은 작동합니다 - 원하는대로 버튼이 표시되지만 OnPreferenceClickListener를 설정할 때 아무 일도 일어나지 않는 문제가 있습니다. 그것은 유발되지 않습니다.사용자 환경 설정 - 버튼을 클릭 할 수 없음

나는 버튼 레이아웃 루트에 beforeDescendants를 추가하려고했지만 행운이 없다. 또한 버튼에 focusable false를 추가하려고했지만 여전히 운이 없다.

누구든지 환경 설정 버튼을 어떻게 반응시킬 수 있는지에 대한 정보를 공유 할 수 있습니까? 감사합니다.

@Override 
protected View onCreateView (ViewGroup parent) { 
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View customRow = inflater.inflate(R.layout.preferences_station_list_row, null); 
    ((ImageButton) customRow.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {  
     @Override 
     public void onClick(View v) { 
      //perform whatever you need to button to do here. 
     } 
    }); 

    customRow.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      showDialog(null); 
     } 
    }); 
    customRow.setClickable(true); 
    return customRow; 
} 

이이 문제를 해결할 수 :

+0

[this] (http://stackoverflow.com/questions/16449372/onpreferenceclick-and-onpreferenceclicklistener) 답변을 확인 했습니까? –

+0

이것이 어떻게 관련이 있는지 모르겠습니다. – Alin

+1

"각 개별 환경 설정에 PreferenceClickListener를 등록해야합니다. somePreference.setOnPreferenceClickListener (this);" 나는 또한 당신의 코드를 게시하는 것이 좋습니다 –

답변

3

단추를 클릭 할 때 Prefererence를 클릭하면 onPreferenceClick이 트리거됩니다. 버튼을 잡으려면 버튼 자체에서 OnClickListener를 설정해야합니다. Preference 클래스에는 findViewById 메소드 또는 이와 유사한 메소드가 없기 때문에 비정상적으로 버튼을 검색하는 것이 문제입니다. 여기에 내가 그것을 할 거라고 방법은 다음과 같습니다

  1. 가에 OnClickListener를 추가 버튼
  2. 을 사용자 정의 기본 설정 클래스를 생성하고 슈퍼 클래스의 팽창 된 레이아웃을 잡아 onCreateView 방법에서 onCreateView
  3. 을 무시하고 찾아 버튼
  4. 사용자의 기호 레이아웃 파일에 사용자 정의 환경 설정 클래스를 사용

이 XML 파일에있는 기본 설정을 대체
public class CustomPreference extends Preference { 

    public CustomPreference(Context context) { 
     super(context); 
    } 

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

    public CustomPreference(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    @Override 
    protected View onCreateView(ViewGroup parent) { 
     View preferenceView = super.onCreateView(parent); 

     setOnClickListener(preferenceView); 

     return preferenceView; 
    } 

    private void setOnClickListener(View preferenceView) { 
     if (preferenceView != null && preferenceView instanceof ViewGroup) { 
      ViewGroup widgetFrameView = ((ViewGroup)preferenceView.findViewById(android.R.id.widget_frame)); 
      if (widgetFrameView != null) { 
       // find the button 
       Button theButton = null; 
       int count = widgetFrameView.getChildCount(); 
       for (int i = 0; i < count; i++) { 
        View view = widgetFrameView.getChildAt(i); 
        if (view instanceof Button) { 
         theButton = (Button)view; 
         break; 
        } 
       } 

       if (theButton != null) { 
        // set the OnClickListener 
        theButton.setOnClickListener(new OnClickListener() { 
         @Override 
         public void onClick(View v) { 
          // do whatever you want to do 
         } 
        }); 
       } 
      } 
     } 
    } 
} 

:

<yourpackage.CustomPreference 
    android:key="your_id" 
    android:title="@string/your_title" 
    android:summary="@string/your_summary"/> 
0

나는 다음이 구현하는 코드와 유사 뭔가를 사용해야합니까?

0

오버라이드 Preference 방법에서 'onClick()'지 확인하십시오 사용자 정의 레이아웃 이되지 않습니다

android:clickable="true" 

특별한 것은 필요 없지만 레이아웃에 a가 포함 된보기가 포함 된 경우 ndroid : clickable = "true" 완전히 망가 뜨립니다.