关于android:带有按钮的EditTextPreference

EditTextPreference With Button

我想在android edittextpreference上放一个按钮。
我创建一个自定义editextpreference:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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 | |按钮|

请帮助我!
谢谢


我将创建DialogPreference的子类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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中,并且将按钮的操作留给您。有关扩展DialogPreference的输入和输出的更多信息,请参见此文章。

为了使密钥进入SharedPreferences,请在XML中放入以下内容:

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


这是基于@gobernador答案的更具体的类,但使用RelativeLayout

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
class EditTextDialogPreference extends DialogPreference {

    //Layout Fields
    private final RelativeLayout layout = new RelativeLayout(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(final Context context, AttributeSet attrs) {
        super(context, attrs);
        setPersistent(true);
        button.setText("Button");
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             //button action
            }
        });
        editText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
    }

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

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) button.getLayoutParams();
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        return layout;
    }

    //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()) {
            String value = editText.getText().toString();
            if (callChangeListener(value))
                persistString(value);
        }

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

        notifyChanged();
    }

    public void setValue(String value) {
        editText.setText(value);
    }
}