2012-05-01 3 views
3

데이터베이스의 항목으로 채워지는 환경 설정 화면이 있습니다. 나는 자신의 PreferenceActivity을 만들어서이 작업을하고있다. 이것은 기본적으로 레이아웃의 오른쪽에 정렬보기에 ImageButton를 추가 활동에서 나는 DialogPreference 항목을 만들고 setLayoutResource(R.layout.custom_pref_row)OnClickListener를 사용자 정의 환경 설정 레이아웃의 버튼에 연결하는 방법은 무엇입니까?

를 사용하여 I 사용자 정의 레이아웃을 사용하고 적용 화면에 기본 설정 항목에 스타일에 내 PreferenceCategory에 추가 . 이것은 모두 잘 작동하고 기본 설정 화면에는 단추가있는 사용자 지정보기가 표시됩니다. 내 질문은 사용자 지정보기의 단추에 클릭 수신기를 연결하는 방법입니다. PreferenceActivity의 행에 대해 View에 도착하는 방법을 찾을 수 없었습니다. 내 항목이 동적으로 작성되지 않은 경우 XML에서이 작업을 모두 수행 한 다음 ID 또는 단추를 참조 할 수 있지만 목록을 동적으로 작성하기 때문에이를 수행 할 수 있습니다.

각 항목의 ImageButton에서 핸들을 얻는 방법에 대한 제안 사항이 있으십니까? 결국 삭제 확인 대화 상자를 시작하도록 단추를 구성하려고합니다.

R.layout.custom_pref_row : 내 PreferenceActivity를의

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:minHeight="?android:attr/listPreferredItemHeight" 
android:gravity="center_vertical" 
android:paddingRight="?android:attr/scrollbarSize"> 

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="15dip" 
    android:layout_marginRight="6dip" 
    android:layout_marginTop="6dip" 
    android:layout_marginBottom="6dip" 
    android:layout_weight="1"> 

    <TextView android:id="@+android:id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:singleLine="true" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:ellipsize="marquee" 
     android:fadingEdge="horizontal" /> 

    <TextView android:id="@+android:id/summary" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@android:id/title" 
     android:layout_alignLeft="@android:id/title" 
     android:textAppearance="?android:attr/textAppearanceSmall" 
     android:maxLines="2" /> 

    <ImageButton android:id="@+id/pref_delete_station" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_trash_can" android:layout_alignParentRight="true" android:background="@null"></ImageButton> 

</RelativeLayout> 

<!-- Preference should place its actual preference widget here. --> 
<LinearLayout android:id="@+android:id/widget_frame" 
    android:layout_width="wrap_content" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical" 
    android:orientation="vertical" /> 


</LinearLayout> 

관련 부분 :

DialogPreference diaPref; 
for (Station mStation : sList) { 
     diaPref = new StationEditor(this.getPreferenceScreen().getContext(), null, this, mStation); 
     diaPref.setLayoutResource(R.layout.custom_pref_row); 
     diaPref.setTitle(mStation.getName()); 
     diaPref.setKey(STATION_PREFIX + mStation.getId()); 

     // add new preference 
     stationTypesCategory.addPreference(diaPref); 
} 

답변

0

좋아요, 쇼팽은 저를 다른 방향으로 생각하게 만들었습니다. Preference 개체가 선택기 화면의 선택기에 나타나는 책임도 있다는 것을 알지 못했습니다.

setLayoutResouce() 함수는 Preference 화면에 표시된 행이 아닌 Dialog 자체의 리소스를 설정합니다. 이것은 혼란 스러웠으며, 나는 이것을 셀렉터 레이아웃에서 조정자 레이아웃을 조정하기 위해 잘못 사용하려고했습니다.

해결책은 onCreateView을 덮어 쓰고 사용자 정의 레이아웃을 반환하는 것입니다. 내게있어서 이것은 직관적이지 못하다. 왜냐하면 그 방법은 보통 대부분의 다른 상황에서 최종 견해를 통제하기 때문이다. 내가 alraedy

내가 처음에 행 자체가 더 이상 클릭 할 수 없었다 그러나 버튼을이었다이었다 우연히 내 Preference (DialogPreference) 그래서 내가해야 할 일을했을 모두가 다음을 추가했다 ...

@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) { 
      Log.i("c","clicked"); 
     } 
    }); 

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

한 문제를 하위 클래스. 전체보기에 수신기를 추가하고 수동으로 호출해야했습니다 ShowDialog(). 지금 누락 된 유일한 것은 기본 설정 화면에서 클릭하면 항목에 더 이상 강조 표시되지 않는다는 것입니다. 어떤 스타일을 적용해야 스타일을 적용 할 수 있습니까?

1

당신은 DialogPreference을 확장하고 onBindDialogView(View view)을 대체 할 수 있습니다. 이 방법의 내부에서 할 수있다 :

@Override 
protected void onBindDialogView(View view) { 

    ((ImageButton) view.findViewById(R.id.pref_delete_station)).setOnClickListener(new OnClickListener() {  
     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

     } 
    }); 

    super.onBindDialogView(view); 
} 

DialogPreference 귀하의 sublcass는 나타내는 항목과 관련된 어떤 국가/값을 저장할 수 있습니다.

DialogPreference을 확장하는 일반적인 지침에 대해서는 this question을 참조하십시오.

희망이 도움이됩니다.

+0

DialogPreference를 이미 하위 클래스로 구현 했으므로 구현하기 쉽습니다. 나는 그것을 시도 할 것이다. 고맙습니다. –

+0

이것은 실행 가능한 솔루션이 아닙니다. 나는 ** DialogPreference 뷰의 버튼에 접근하려고하지 않는다 **. 대화 상자를 시작하는 환경 설정 화면의 일부인 단추를 가져 오려고합니다. –

+0

쇼팽, 내게 올바른 방향으로 생각하게 해 줬어. 옳은 생각이지만 잘못된 기능. 대신 onCreateView를 재정의해야했습니다. 몇 가지 문제가있었습니다. 다른 4 시간 동안 내 자신의 질문에 답할 수는 없지만 가능한 한 완벽한 해결책을 게시 할 것입니다. –

관련 문제