的EditText preference巴顿 [英] EditTextPreference With Button

查看:183
本文介绍了的EditText preference巴顿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望把Android上的EditText preference一个按钮。
我创建一个自定义editext preference:

I want put a button on android edittextpreference. I create a custom editextpreference:

public class EditTextPreferenceWithButton extends EditTextPreference {

    private Context context;

    public EditTextPreferenceWithButton(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.context=context;
      }

      public EditTextPreferenceWithButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
      }

      public EditTextPreferenceWithButton(Context context) {
        super(context);
        this.context=context;
      }



      @Override
        protected void onBindDialogView(View view) {
            super.onBindDialogView(view);



           view.setLayoutParams(new LayoutParams(android.view.ViewGroup.LayoutParams.WRAP_CONTENT, android.view.ViewGroup.LayoutParams.WRAP_CONTENT));



            final EditText editText = (EditText)view.findViewById(android.R.id.edit);
            ViewGroup vg = (ViewGroup)editText.getParent();

            Button button = new Button(context);


            vg.addView(button,ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT);


        }
}

在这样的按钮显示编辑文本的下方,但我想它是下一个像这样的EditText:

In this way the button is show below the edit text, but I want it is next the edittext like this:

|的EditText | |按钮|

|EditText| |Button|

请帮帮我!
谢谢

Please help me! Thank you

推荐答案

我想创建的一个子类对话框preference

class EditTextDialogPreference extends DialogPreference {

    //Layout Fields
    private final LinearLayout layout = new LinearLayout(this.getContext());
    private final EditText editText = new EditText(this.getContext());
    private final Button button = new Button(this.getContext());


    //Called when addPreferencesFromResource() is called. Initializes basic paramaters
    public EditTextDialogPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(true);
        button.setText("Button");
        layout.setOrientation(LinearLayout.HORIZONTAL);
    }

    //Create the Dialog view
    @Override
    protected View onCreateDialogView() {
        layout.addView(editText);
        layout.addView(button);
        return parentLayout;
    }

    //Attach persisted values to Dialog
    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        editText.setText(getPersistedString("EditText"), TextView.BufferType.NORMAL);
    }

    //persist values and disassemble views
    @Override
    protected void onDialogClosed(boolean positiveresult) {
        super.onDialogClosed(positiveresult);
        if (positiveresult && shouldPersist()) {
            persistString(editText.getText().toString());
        }

        ((ViewGroup) editText.getParent()).removeView(editText);
        ((ViewGroup) button.getParent()).removeView(button);
        ((ViewGroup) layout.getParent()).removeView(layout);

        notifyChanged();
    }
}

我假设你会在的EditText 来坚持的价值,我要离开按钮的操作取决于你。请参见这个帖子关于中的进出的延长对话preference的

I'm assuming you're going to persist the value in the EditText and I'm leaving the action of the button up to you. See this post for more information on the in's and out's of extending DialogPreference.

为了拿到钥匙到共享preferences ,把你的XML如下:

In order to get the key into the SharedPreferences, put the following in your XML:

<com.yourpackage.EditTextDialogPreference
    android:key="Your Key"
    android:persistent="true"/>

这篇关于的EditText preference巴顿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆