我如何创建一个preference有一个EditText preference和一个切换按钮?创建一个、有一个、按钮、preference

2023-09-06 00:16:19 作者:很久没笑了

我试图实现基本和详细的下方(在preferences,我已经平方)图像的副本。 pressing什么的preference应该打开一个对话框左侧。 pressing切换按钮将启用/禁用无论我在这个preference我的设置。

What I'm trying to implement is basically and exact replica of the image below (the preferences that I've squared). Pressing anything to the left of the preference should open up a dialog. Pressing the togglebutton will disable/enable whatever I'm setting in this preference.

我一直在尝试了几个小时,现在我已经拿出空手而归。在preferenceActivity如何实现这一点?

I've been trying for hours now and I've come up empty-handed. How do I implement this in a PreferenceActivity?

编辑:看来人们都误解我的问题。我想出了如何使用preferenceActivity解决我的问题,这是非常重要的。不是活动。我不在乎我是否需要做的是在XML或编程。由我提供,我不能一个或类似的东西中使用的答案只是请不要。的

编辑2:新增一个赏金 - 我真的需要一个答案的

推荐答案

地狱的人,我喜欢你的想法: - )

Hell man, I like your idea :-)

这仅仅是同@ MH的答案,但更简洁。

This is just same as @MH's answer, but more concise.

我用切换按钮测试,而不是开关

package android.dumdum;

import android.content.Context;
import android.preference.Preference;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

public class TogglePreference extends Preference {

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

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

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

    public View getView(View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = new LinearLayout(getContext());
            ((LinearLayout) convertView)
                    .setOrientation(LinearLayout.HORIZONTAL);

            TextView txtInfo = new TextView(getContext());

            txtInfo.setText("Test");
            ((LinearLayout) convertView).addView(txtInfo,
                    new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.FILL_PARENT,
                            LinearLayout.LayoutParams.WRAP_CONTENT, 1));

            ToggleButton btn = new ToggleButton(getContext());
            ((LinearLayout) convertView).addView(btn);
        }

        return convertView;
    }
}

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <PreferenceCategory android:title="Test custom preferences" >
        <android.dumdum.EncryptorEditTextPreference />
        <android.dumdum.TogglePreference />
    </PreferenceCategory>

</PreferenceScreen>

EncryptorEditText preference 不涉及您的问题,但它使用同样的技术(延长的EditText preference )。

EncryptorEditTextPreference is not related to your question, but it uses same technique (extending EditTextPreference).

这说明:

 
精彩推荐
图片推荐