2

嗨,我想在 prefrencescreen 中添加一个按钮,我成功地将一个按钮添加到 prefrence 中,但我无法获得 onClick 事件。我已经附上了我的代码一张偏好屏幕的图片

设置.xml

<PreferenceCategory android:title="Application Details">

    <Preference android:key="type" 
                android:title="Type"
                android:summary="" />

</PreferenceCategory>

<PreferenceCategory android:title="Notification Settings">

    <ListPreference android:key="sendNotificationType"
                    android:title="Status Notification For"
                    android:dialogTitle="Status Notification For" />

</PreferenceCategory>

设置细节.xml

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/textView2"
    android:layout_marginLeft="15dp"
    android:text="Type"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView1"
    android:layout_marginLeft="15dp"
    android:layout_toLeftOf="@+id/setFromTimeBtn"
    android:text="Summary"
    android:textAppearance="?android:attr/textAppearanceSmall" />

<Button
    android:id="@+id/buyItNowBtn"
    android:layout_width="80dp"
    android:layout_height="30dp"
    android:layout_alignParentRight="true"
    android:layout_marginRight="15dp"
    android:layout_centerVertical="true"
    android:background="@drawable/button"
    android:text="@string/buyItNowBtnTxt"
    android:textColor="@color/white" />

以及 prefenceActivity 类的 onCreate 方法

protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.layout.setting);
    setContentView(R.layout.settingview);

    Preference typePref = (Preference) findPreference("type");
    typePref.setLayoutResource(R.layout.settingdetail);
typePref.setSelectable(true);

    Button btn = (Button) findViewById(R.id.buyItNowBtn);
    btn.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        Log.e(TAG,"TEST");
        Toast.makeText(Setting.this, "TEST", Toast.LENGTH_SHORT).show();
    }
});

}

截屏

在此处输入图像描述

4

5 回答 5

5

该线程已过时,但由于尚无解决方案,因此我成功的方法如下:

1.设置按钮点击监听

有两种方法可以为按钮设置单击侦听器,以编程方式或通过 XML。

以编程方式:在您的自定义首选项中,覆盖onBindView并附加监听器:

@Override
protected void onBindView(View view) {
    View button = view.findViewById(R.id.myButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d(LOG_TAG, "button click listener works!");
        }
    });
    super.onBindView(view);
}

通过 XML:添加android:onClick="yourMethodName"Button元素的布局定义(这是您偏好的布局的一部分,见下文)并PreferenceActivity按照此处所述实现您的方法:响应点击事件

2.修复OnPreferenceClickListener

现在按钮单击侦听器应该可以工作,但首选项OnPreferenceClickListener将不再工作。为了解决这个问题,添加

android:descendantFocusability="blocksDescendants"

根据您的喜好到布局文件中的顶部元素:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?android:attr/listPreferredItemHeight"
    android:gravity="center_vertical"
    android:paddingEnd="?android:attr/scrollbarSize"
    android:background="?android:attr/selectableItemBackground"
    android:descendantFocusability="blocksDescendants">

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="15dip"
        android:layout_marginEnd="6dip"
        android:layout_marginTop="6dip"
        android:layout_marginBottom="6dip"
        android:layout_weight="1">

        <TextView android:id="@android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:singleLine="true"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:ellipsize="marquee"
            android:fadingEdge="horizontal" />

        <TextView android:id="@android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@android:id/title"
            android:layout_alignStart="@android:id/title"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="?android:attr/textColorSecondary"
            android:maxLines="4" />

        <Button android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true" />

    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->
    <LinearLayout android:id="@android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:orientation="vertical" />
</LinearLayout>

android:onClick="yourMethodName"这里不,因为我选择以编程方式进行。)

3.附加布局

最后,您需要将此布局设置为您的首选项 XML 文件:android:layout="@layout/myLayoutName"setLayoutResource您的PreferenceFragment.

于 2014-11-18T12:46:48.140 回答
2

在preferenceActivity中实现OnPreferenceClickListener

private Preference mBuyPreference = null;
mLogoutPreference = findPreference(yourkey);
mBuyPreference.setOnPreferenceClickListener(this);

@Override
public boolean onPreferenceClick(Preference preference) {
if (preference == mBuyPreference) {

    try {
        //Do Something
    }

    return false;
}
于 2012-05-09T10:43:37.783 回答
0

通过创建这样的自定义首选项,我已经解决了一半的问题 -

为了达到这个目的,我创建了一个这样的自定义首选项 -

public class CustomPreference extends Preference {

private LinearLayout mWidgetContainer;
private View mRowView;

public CustomPreference(Context context) {
    super(context);
}

public CustomPreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected View onCreateView(ViewGroup parent) {
    LayoutInflater viewInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    mRowView = viewInflater.inflate(R.layout.preferences_row_view, parent, false);

    mWidgetContainer = (LinearLayout) mRowView.findViewById(android.R.id.widget_frame);

    Button button = new Button(getContext());
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    button.setLayoutParams(params);
    button.setBackgroundResource(R.drawable.listview_row_bg);
    button.setTextSize(14);
    button.setPadding(5, 5, 5, 5);
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(getContext(), BuyScreenActivity.class);
            getContext().startActivity(intent);
        }
    });
    button.setTypeface(null, Typeface.BOLD);
    button.setText("Buy now");
    mWidgetContainer.addView(button);

    return mRowView;
}
}

然后在您的设置 xml 文件中,您可以创建一个设置,如

<package.of.your.java.file.CustomPreference 
    android:key="type" 
    android:title="Type"
    android:summary="" />

而我的preferences_row_view.xml 是

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:minHeight="50dip"
    android:paddingLeft="5dip"
    android:paddingRight="?android:attr/scrollbarSize" >

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="6dip"
        android:layout_marginRight="6dip"
        android:layout_marginTop="6dip"
        android:layout_weight="1" >

        <TextView
            android:id="@+android:id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:maxLines="2"
            android:lineSpacingMultiplier="1.3"
            android:textSize="14sp" />

        <TextView
            android:id="@+android:id/summary"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@android:id/title"
            android:layout_below="@android:id/title"
            android:maxLines="4"
            android:textSize="13sp" />
    </RelativeLayout>

    <!-- Preference should place its actual preference widget here. -->

    <LinearLayout
        android:id="@+android:id/widget_frame"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:gravity="center_vertical"
        android:orientation="horizontal" />

</LinearLayout>

那会做的。

但是我还面临一个问题,我已经在 SO 上发布了这个问题。

在首选项行中添加一个按钮

于 2013-01-07T11:32:27.753 回答
0

不要使用 onClick() 处理程序,而是覆盖 PreferenceActivity 中的 onPreferenceTreeClick 方法。在那里,您将获得单击的首选项对象,并且可以检查其键。

public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {

 if (preference.getKey().equals("key")) {           
       // do something
       return true;
 }

 return false;
}
于 2012-05-09T10:30:13.063 回答
0

代替

 Button btn = (Button) findViewById(R.id.buyItNowBtn);

做这个

  Button btn = (Button)typePref.getView(view, viewGroup );
于 2016-01-19T04:50:24.753 回答