Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

android - Custom PreferenceScreen with clickable buttons

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

reslayoutsetting.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>

resxmlpref.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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...