繁体   English   中英

自定义首选项屏幕,带有可单击的按钮

[英]Custom PreferenceScreen with clickable buttons

我创建了以下自定义首选项屏幕。 在此输入图像描述

我想在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>

你需要:

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.");
    }
});

我没有答案,但有一些见解 -

您尝试查找按钮的视图不是settings.xml(或pref.xml) - 这就是findViewById返回null的原因。

我认为加载的视图(布局)是内部Android OS视图,ID为17367111(或0x1090047)。

您打开的PreferenceScreen中唯一的首选项是另一个PreferenceScreen(使用settings.xml作为布局)

您需要访问此内部PreferenceScreen的视图以及您能够执行的操作:

prefScreeView.findViewByID(R.id.button1);

希望它能帮助某人找到答案。

按钮btn =(按钮)getPreferenceScreen()。findPreference(key);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM