Android:使用值列表创建自定义视图
我正在尝试创建一个带有值列表的类似微调器的自定义视图。我已经设法用下面的代码开始了Android:使用值列表创建自定义视图,android,android-custom-view,android-databinding,Android,Android Custom View,Android Databinding,我正在尝试创建一个带有值列表的类似微调器的自定义视图。我已经设法用下面的代码开始了 public class SelectionTextView extends TextInputEditText implements View.OnClickListener { private CharSequence[] entries, values; private CharSequence value; @Override public void onClick(V
public class SelectionTextView extends TextInputEditText implements View.OnClickListener {
private CharSequence[] entries, values;
private CharSequence value;
@Override
public void onClick(View v) {
new AlertDialog.Builder(v.getContext())
.setTitle("Title")
.setItems(entries, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
value = values[which];
SelectionTextView.super.setText(entries[which]);
}
})
.create()
.show();
}
public SelectionTextView(final Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
super.setCursorVisible(false);
super.setFocusable(false);
super.setFocusableInTouchMode(false);
super.setInputType(InputType.TYPE_NULL);
super.setOnClickListener(this);
}
public void setEntries(CharSequence[] entries) {
this.entries = entries;
super.setOnClickListener(this);
}
public void setValues(CharSequence[] values) {
this.values = values;
}
public void setValue(Object value) {
this.value = value.toString();
}
public CharSequence getValue() {
return value;
}
}
然而,我想实现一些类似于onValueChanged、onEntryChanged的东西。我该怎么做呢?另外,如何通过Android数据绑定使value属性可绑定
谢谢你的帮助
更新:2018年3月13日
发布我的完整和有效选择TextView.class
package com.mycompany.myproject;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.util.AttributeSet;
import android.view.View;
public class SelectionTextView extends TextInputEditText implements View.OnClickListener {
private CharSequence[] entries, values;
private String value;
private String prompt;
private OnValueChangedListener listener;
@Override
public void onClick(View v) {
new AlertDialog.Builder(v.getContext())
.setTitle(prompt)
.setItems(entries, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
setValue(values[which].toString());
}
})
.create()
.show();
}
public SelectionTextView(final Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
super.setCursorVisible(false);
super.setFocusable(false);
super.setFocusableInTouchMode(false);
super.setInputType(InputType.TYPE_NULL);
super.setOnClickListener(this);
TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SelectionTextView, 0, 0);
try {
entries = typedArray.getTextArray(R.styleable.SelectionTextView_entries);
values = typedArray.getTextArray(R.styleable.SelectionTextView_values);
value = typedArray.getString(R.styleable.SelectionTextView_value);
prompt = typedArray.getString(R.styleable.SelectionTextView_prompt);
} finally {
typedArray.recycle();
}
}
public void setOnValueChangeListener(OnValueChangedListener listener) {
setValue(this.value);
}
public void setEntries(CharSequence[] entries) {
this.entries = entries;
invalidate();
}
public void setValues(CharSequence[] values) {
this.values = values;
invalidate();
}
public void setValue(String value) {
this.value = value;
if (value != null) {
if (entries != null && values != null) {
for (int i = 0; i < entries.length; i++) {
if (values[i].toString().equals(value)) {
super.setText(entries[i].toString());
invalidate();
break;
}
}
}
}
}
public void setValue(Integer value) {
if (value != null) {
setValue(value.toString());
}
}
public String getValue() {
return value;
}
public interface OnValueChangedListener {
void onValueChange(SelectionTextView view, String value);
}
}
在
AttributeSet
构造函数中,添加以下内容:
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.SelectionTextView, 0, 0);
value = a.getString(R.styleable.SelectionTextView_value, "");
在res\values
文件夹中,添加包含以下内容的attrs.xml
:
<resources>
<declare-styleable name="SelectionTextView">
<attr name="value" format="string" />
</declare-styleable>
</resources>
对于
onValueChanged
,只需定义一个具有onValueChanged方法的接口和一个以该接口为参数的“register”方法,并将其存储在数据成员中。然后,在setValue
方法中,调用该接口。谢谢。用我的最终自定义视图类更新了我的帖子。
<resources>
<declare-styleable name="SelectionTextView">
<attr name="value" format="string" />
</declare-styleable>
</resources>