android - 添加一个按钮到 PrefrenceScreen : Android

标签 android onclick android-preferences onclicklistener

您好,我想在 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();
    }
});

}

截图

enter image description here

最佳答案

这个线程已经过时了,但是由于还没有解决方案,所以我是这样成功的:

<强>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 中实现该方法,如下所述:Responding to Click Events

<强>2。修复 OnPreferenceClickListener

现在按钮点击监听器应该可以工作了,但是 Preference 的 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" 或在 PreferenceFragment 中构建首选项时通过 setLayoutResource 进行设置。

关于android - 添加一个按钮到 PrefrenceScreen : Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10514005/

相关文章:

android - 在 Android PreferenceActivity 中应用复选框样式

android - OpenGL ES2.0 glReadPixels() 非常慢

javascript - cakephp onclick更改图像,放置id和函数调用

javascript - 使用javascript禁用onclick属性

android - 无法获取设置按钮以显示动态壁纸

android - 如何获取 Android 首选项的类别?

java - android - 多次运行计时器倒计时错误

android - 无法在 Windows 10 上卸载 Android Studio

java - Android Studio 如何在屏幕上打印可变内容(数字)

javascript - jquery,防止父元素上的 onclick=window.location