2011-03-17 161 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); 

希望它可以帮助别人找到答案。