我需要我的微调器有两个不同的文本颜色(绿色= ok,红色=不正常),具体取决于" mastervalue"。
例如:
如果 mastervalue = 1 :
Spinner 1
Spinner 2
如果 mastervalue = 2 :
Spinner 1
Spinner 2
因此,每次更改主值时,颜色都必须动态更改。要检查哪些值是绿色/红色,我的数据库中有几个多对多关系表。我只需找到一种方法来为填充微调器之前/之后的每个值设置文本颜色。
很高兴在这里得到一些帮助!
答案 0 :(得分:2)
<强> spinner_item.xml:强>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="@dimen/button_height"
/>
<强> MyParameter.java 强>
public class MyParameter {
public String value;
public boolean allowed;
public MyParameter(){
super();
}
public MyParameter(final String value, final boolean allowed) {
super();
this.value = value;
this.allowed = allowed;
}
}
<强> MyParameterAdapter.java 强>
public class MyParameterAdapter extends ArrayAdapter<MyParameter> {
Context ctx;
MyParameter[] values;
public MyParameterAdapter(Context ctx, MyParameter[] values) {
super(ctx, R.layout.li_spinner_item, values);
this.ctx = ctx;
this.values = values;
}
@Override public View getDropDownView(int position, View view, ViewGroup parent) {
return getCustomView(position, view, parent);
}
@Override public View getView(int pos, View view, ViewGroup parent) {
return getCustomView(pos, view, parent);
}
public View getCustomView(int position, View view, ViewGroup parent) {
View spinner = LayoutInflater.from(ctx).inflate(R.layout.spinner_item, parent, false);
TextView txtValue = (TextView) spinner.findViewById(android.R.id.text1);
if(values[position].allowed){
spinner.setBackgroundColor(Color.GREEN);
} else {
spinner.setBackgroundColor(Color.RED);
}
txtValue.setText(values[position].value);
return spinner;
}
}
在我填充微调器之前,我使用以下函数来标记值:
public static MyParameter[] markValues(final List<String> allValues, final List<String> matchingValues) {
MyParameter[] values = new MyParameter[allValues.size()];
for(int i=0; i<allValues.size(); i++){
final MyParameter param = new MyParameter();
param.value = allValues.get(i);
param.allowed = matchingValues.contains(allValues.get(i));
values[i] = param;
}
return values;
}
答案 1 :(得分:1)
对于您的问题,您只需创建一个简单的自定义SpinnerAdapter并根据需要进行操作:
1.首先,让我们创建一个custom_spinner行(布局)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text_main_seen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
public class MyAdapter extends ArrayAdapter<String> {
String[] spinnerValues;
public MyAdapter(Context ctx, String[] objects) {
super(ctx, R.layout.custom_spinner, objects);
spinnerValues = objects;
}
@Override public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
@Override public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent, false);
TextView main_text = (TextView) mySpinner .findViewById(R.id.text_main_seen);
//here you make the algorithm you want for coloration///////////////////////////
if(spinnerValues[position].equals("blue")){
mySpinner.setBackgroundColor(Color.BLUE);
}else if(spinnerValues[position].equals("green")){
mySpinner.setBackgroundColor(Color.GREEN);
}else if(spinnerValues[position].equals("red")){
mySpinner.setBackgroundColor(Color.RED);
}else if(spinnerValues[position].equals("yellow")){
mySpinner.setBackgroundColor(Color.YELLOW);
}
//end coloration algorithm//////////////////////////////////////////
main_text.setText(spinnerValues[position]);
return mySpinner;
}
}
最后在你的活动中,你在你的微调器中引用这个适配器:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//values to put in the spinner
String[] values = { "blue", "red", "green", "yellow" };
//my spinner
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
//set MyAdaper
mySpinner.setAdapter(new MyAdapter(this, values));
}
}
最后,如果需要,这是一个简单的测试;)
祝你好运,如果出现任何问题,请告诉我;)