我想显示当前日期和未来5天的日期和星期,因此我正在使用动态视图,并以编程方式将此视图添加到父视图。现在,我想在onclick操作中更改文本视图的颜色和视图的背景颜色。首先,我需要更改第一个索引的颜色。 我正在分享我的代码,谁能告诉我onclick时如何更改textview的文本颜色。
maiin.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:padding="8dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="15sp"
android:id="@+id/datenumber"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="15sp"
android:inputType="textCapSentences"
android:capitalize="sentences"
android:layout_marginTop="14dp"
android:layout_gravity="center"
android:id="@+id/weekname" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textSize="15sp"
android:visibility="gone"
android:inputType="textCapSentences"
android:capitalize="sentences"
android:layout_marginTop="14dp"
android:layout_gravity="center"
android:id="@+id/date" />
</LinearLayout>
</LinearLayout>
活动:(日历日期为从当前日期到接下来的5天)
public class TableBookActivtiy extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maiin);
SimpleDateFormat sdf = new SimpleDateFormat("EEE dd");
for (int i = 0; i < 6; i++) {
Calendar calendar = new GregorianCalendar();
calendar.add(Calendar.DATE, i);
String day = sdf.format(calendar.getTime());
String[] splited = day.split("\\s+");
final View child = getLayoutInflater().inflate(R.layout.activitiy_tabledate, null);
final TextView datenumber = (TextView) child.findViewById(R.id.datenumber);
final TextView weekname = (TextView) child.findViewById(R.id.weekname);
final TextView date = (TextView) child.findViewById(R.id.date);
child.setTag(date.getText().toString());
datenumber.setText(splited[0]);
weekname.setText(splited[1]);
dates_lay.addView(child);
child.setOnClickListener(new OnClickLitener());
}
((View) dates_lay.getChildAt(0)).setBackgroundColor(Color.RED); //for index 0
}
class OnClickLitener implements View.OnClickListener {
@Override
public void onClick(View v) {
// date number week name date// want to change text and background colours
}
}
}
答案 0 :(得分:0)
mTextView.setTextColor(Color.parseColor("#bdbdbd"));
或者,如果您在资源的color.xml文件中定义了颜色代码,则
(来自API> = 23)
mTextView.setTextColor(ContextCompat.getColor(context, R.color.<name_of_color>));
(对于API <23)
mTextView.setTextColor(getResources().getColor(R.color.<name_of_color>));