我想在微调器中更改所选文本的颜色。无论如何我还可以将边界改为白色。数字选择器的同样问题我想要显示数字的颜色白色或者是否被选中,因为我项目中的所有文本都是白色
package myfyp.pkg.futureadvisor;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.widget.TextView;
public class Enter_Info_Activity extends Activity {
TextView t1,t2,t3;
EditText e1,e2;
Button b1;
private Spinner spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.enterinfor);
t1=(TextView) findViewById(R.id.textView1);
t2=(TextView) findViewById(R.id.programe);
e1=(EditText) findViewById(R.id.editText1);
t3=(TextView) findViewById(R.id.percentage);
addItemsOnSpinner2();
NumberPicker np = (NumberPicker) findViewById(R.id.np);
np.setMinValue(0);
//Specify the maximum value/number of NumberPicker
np.setMaxValue(100);
//Gets whether the selector wheel wraps when reaching the min/max value.
np.setWrapSelectorWheel(true);
//Set a value change listener for NumberPicker
b1 = (Button) findViewById(R.id.search);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intenet=new Intent(v.getContext(),List_item.class);
startActivityForResult(intenet,0);
}
});
}
public void addItemsOnSpinner2() {
spinner2 = (Spinner) findViewById(R.id.spinner2);
List<String> list = new ArrayList<String>();
list.add("Bachelor of Architecture");
list.add("Bachelor of Business Administration");
list.add("Bachelor of Commerce");
list.add("Bachelor of Computer Information Systems");
list.add("Bachelor of Science in Construction Technology");
list.add("Bachelor of Criminal Justice");
list.add("Bachelor of Economics");
list.add("Bachelor of Education");
list.add("Bachelor of Engineering");
list.add("Bachelor of Fine Arts");
list.add("Bachelor of Information Systems");
list.add("Bachelor of Social Work");
list.add("Bachelor of Technology");
list.add("Bachelor of Accountancy");
list.add("Bachelor of Science in Aerospace Engineering");
list.add("Bachelor of Science in Architecture");
list.add("Bachelor of Science in Architectural Engineering");
list.add("Bachelor of Science in Business Administration");
list.add("Bachelor of Science in Business and Technology");
list.add("Bachelor of Science in Chemical Engineering");
list.add("Bachelor of Science in Chemistry");
list.add("Bachelor of Science in Civil Engineering");
list.add("Bachelor of Science in Computer Engineering");
list.add("Bachelor of Science in Computer Science");
list.add("Bachelor of Science in Criminal Justice");
list.add("Bachelor of Science in Electrical Engineering");
list.add("Bachelor of Science in Engineering Technology");
list.add("Bachelor of Science in English Literature");
list.add("Bachelor of Science in Environmental Science");
list.add("Bachelor of Science in History");
list.add("Bachelor of Science in Human Resources Management");
list.add("Bachelor of Science in Industrial Engineering");
list.add("Bachelor of Science in Information Technology");
list.add("Bachelor of Science in Information Systems");
list.add("Bachelor of Science in Information Systems");
list.add("Bachelor of Science in Integrated Science");
list.add("Bachelor of Science in Integrated Science");
list.add("Bachelor of Science in International Relations");
list.add("Bachelor of Science in Journalism");
list.add("Bachelor of Science in Manufacturing Engineering");
list.add("Bachelor of Science in Marketing");
list.add("Bachelor of Science in Mathematics");
list.add("Bachelor of Science in Mechanical Engineering");
list.add("Bachelor of Science in Mining Engineering");
list.add("Bachelor of Science in Software Engineering");
list.add("Bachelor of Science in Physics");
list.add("Bachelor of Science in Politics");
list.add("Bachelor of Science in Psychology");
list.add("Bachelor of Science in Real-Time Interactive Simulation");
list.add("Bachelor of Science in Religion");
list.add("Bachelor of Science in Risk Management and Insurance");
list.add("Bachelor of Science in Science Education");
list.add("Bachelor of Science in Systems Engineering");
list.add("Bachelor of Science in Veterinary Technology");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(dataAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activity1, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.aboutusid) {
Intent aboutusintent = new Intent(this,Aboutus_Activity.class);
startActivity(aboutusintent);
return true;
}
else if (id == R.id.helpid) {
Intent helpIntent = new Intent(this,Help_Activity.class);
startActivity(helpIntent);
return true;
}
else if (id == R.id.homeid) {
Intent mainIntent = new Intent(this,MainActivity.class);
startActivity(mainIntent);
return true;}
else if (id == R.id.exitid) {
finish();
System.exit(0);
}
else if (id == R.id.exitid) {
return false;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
您可以使用ColorStateList
在Adapter
的布局中执行此操作。您可以在ColorStateList
资源文件夹中使用xml创建color
,如下所示:
<?xml version "1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android/com/apk/res/android">
<item android:state_checked="true" android:color="?attr/colorAccent"/>
<item android:state_selected="true" android:color="?attr/colorAccent"/>
<item android:state_pressed="true" android:color="?attr/colorAccent"/>
<item android:state_activated="true" android:color="?attr/colorAccent"/>
<item android:color="?android:attr/textColorPrimary"/>
</selector>
然后你可以使用ColorStateList
创建一个布局(我在这里使用它作为文本颜色,但你可以设置任何可以使用颜色资源的属性):
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:padding="16dp" android:textSize="16sp"
android:textColor="@color/selector_accent"/>
最后,将此布局设置为下拉视图资源:
dataAdapter.setDropDownViewResource(R.layout.view_selectable);
对于NumberPicker
,它看起来不像设置布局的标准方法,但您应该能够根据this answer更改文本的颜色。然后使用ColorStateList
:
editText.setTextColor(ContextCompat.getColorStateList(this, R.color.selector_accent));