簡體   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