4

다음 사용자 정의 환경 설정 화면이 생성되었습니다. enter image description here클릭 가능한 버튼이있는 사용자 정의 환경 설정 화면

Button1과 Button2에 Listener를 추가하려고합니다. 어떻게해야합니까?

위의 환경 설정 화면을 만들 때 다음 코드를 사용하고 있습니다.

DemoPref :

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 

public class DemoPref extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     startActivity(new Intent(this, MyPref.class)); 
    } 
} 

MyPref :

import android.os.Bundle; 
import android.preference.PreferenceActivity; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MyPref extends PreferenceActivity{ 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     //setContentView(R.layout.tftp_setting); 
     this.addPreferencesFromResource(R.xml.list_pref); 
     /*Button b1 = (Button)findViewById(R.id.button1); 
     b1.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 

       Log.i("MyPref", "Button1 one is clicked."); 
      }   
     }); 

     Button b2 = (Button)findViewById(R.id.button2); 
     b2.setOnClickListener(new OnClickListener(){ 

      @Override 
      public void onClick(View v) { 

       Log.i("MyPref", "Button2 one is clicked."); 
      }   
     });*/ 
    } 
} 

고해상도 \ 레이아웃 \의 setting.xml :

<?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="fill_parent" 
    android:orientation="vertical" 
    > 
    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> 

    <TextView android:text="TextView1" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
    <EditText android:text="EditText2" android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content"></EditText> 

    </LinearLayout> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> 

    <TextView android:text="TextView2" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView> 
    <EditText android:text="EditText2" android:id="@+id/editText2" android:layout_height="wrap_content" android:layout_width="wrap_content"></EditText> 

    </LinearLayout> 

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> 

    <Button android:text="Button1" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 
    <Button android:text="Button2" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button> 

    </LinearLayout> 
</LinearLayout> 

고해상도 \ XML \ pref.xml :

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

<PreferenceScreen android:title="My Setting" android:layout="@layout/setting" android:summary="This is my setting."></PreferenceScreen> 

</PreferenceScreen> 

답변

1

는 당신이 필요합니다.

Button b1 = (Button) findViewById(R.id.button1); 
Button b2 = (Button) findViewById(R.id.button2); 

b1.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     Log.i("MyPref", "Button1 one is clicked."); 
    } 
}); 

b2.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
     Log.i("MyPref", "Button2 one is clicked."); 
    } 
}); 
+0

나는 이미 시도했지만 널 포인터 예외를 던지고있다. 그 때문에 설정 한 콘텐츠 뷰를 추가했지만 예외도 던지고 있습니다 – Vivek

+0

@Override는 컴파일러 힌트 일 뿐이며 컴파일 된 코드에는 영향을주지 않습니다 – dkneller

-1

버튼 BTN = (버튼) getPreferenceScreen() findPreference (키);

+1

버튼에 환경 설정을 캐스트 할 수 없습니다. – Vivek

0

나는 대답을 가지고 있지만 몇 가지 통찰력이없는 -

당신이 버튼을 찾기 위해 노력하는 뷰가 Settings.XML의 (또는 pref.xml)이 아니다 - findViewById를 null을 반환 이유입니다을.

로드 된 뷰 (레이아웃)가 ID 17367111 (또는 0x1090047) 인 내부 Android OS보기라고 생각합니다.

당신이 PreferenceScreen에있는 유일한 선호 열고 당신이 내부 PreferenceScreen의 전망에 접근 할 필요가

(레이아웃과 Settings.XML의 사용) 다른 PreferenceScreen하고 그들 당신은 수있을 것 해야 할 일 :

prefScreeView.findViewByID(R.id.button1); 

누군가가 답을 찾는데 도움이되기를 바랍니다.

관련 문제