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

发布于 2024-10-22 22:31:47 字数 3760 浏览 2 评论 0原文

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

我想在 Button1 和 Button2 上添加监听器。我该怎么做呢?

我正在使用以下代码来创建上述首选项屏幕。

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\layout\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>

I have created following Custom Preference Screen.
enter image description here

I want to add Listener on Button1 and Button2. How should I do it?

I am using following code for creating above Preference Screen.

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\layout\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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

堇年纸鸢 2024-10-29 22:31:47

您需要:

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

You need:

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.");
    }
});
椒妓 2024-10-29 22:31:47

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

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

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

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

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

prefScreeView.findViewByID(R.id.button1);

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

I don't have an answer but have some insight -

The view in which you are trying to find the buttons is NOT settings.xml (or pref.xml) - that's why findViewById returns null.

I think the view (layout) which is loaded is internal Android OS view, with ID 17367111 (or 0x1090047).

The only preference you have in the PreferenceScreen you open is another PreferenceScreen (which uses the settings.xml as layout)

you need to get access to the view of this internal PreferenceScreen and them you'd be able to do:

prefScreeView.findViewByID(R.id.button1);

Hope it helps someone to find the answer.

一身骄傲 2024-10-29 22:31:47

按钮 btn = (Button) getPreferenceScreen().findPreference(key);

Button btn = (Button) getPreferenceScreen().findPreference(key);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文