2011-03-17 159 views
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."); 
      }   
     });*/ 
    } 
} 

RES \佈局\ 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> 

RES \ 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 2011-03-17 03:28:31

+0

@Override只是一個編譯器提示,並不影響編譯代碼 – dkneller 2013-09-13 02:30:52

-1

按鈕BTN =(按鈕)getPreferenceScreen()findPreference(密鑰);

+1

不能將偏好轉換爲按鈕 – Vivek 2011-03-17 06:02:17

0

我沒有一個答案,但有一些見解 -

中,你正在努力尋找按鈕的看法是不settings.xml中(或pref.xml) - 這就是爲什麼findViewById返回null。

我認爲加載的視圖(佈局)是內部Android操作系統視圖,ID爲17367111(或0x1090047)。

您在PreferenceScreen具有唯一的首選項,你打開另外一個PreferenceScreen(使用settings.xml中的佈局)

您需要獲得訪問該內部PreferenceScreen的觀點,你會是他們能夠要做:

prefScreeView.findViewByID(R.id.button1); 

希望它可以幫助別人找到答案。