我有一个动态,我已动态创建了一个表。该表是从SQLite数据库填充的。单击表格标题,表格将相应地进行排序。
一切正常,现在我的要求是在单击时更改表头文本视图的文本颜色。文字颜色没有改变。
这是填充表格的方法:
//Time ascending method
public void populateTableTimeAscending()
{
getTimeNameTimeAsc();
getStudentNameTimeAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_asc.iterator();
Iterator itr2 = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
在 time_name_title.setOnClickListener(new OnClickListener()里面,我试图更改未发生的文字颜色。
----------------编辑完整代码------------------------- ---------
public class TableActivity extends Activity {
TableLayout follow_up_table;
TableRow followup_tr_data;
List<String> time_name_list_time_asc;
List<String> student_name_list_time_asc;
List<String> time_name_list_time_dsc;
List<String> student_name_list_time_dsc;
List<String> time_name_list_studname_asc;
List<String> student_name_studname_asc;
List<String> time_name_list_studname_dsc;
List<String> student_name_list_studname_dsc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_table);
follow_up_table=(TableLayout) findViewById(R.id.follow_up_table);
//getTime();
// database handler
deleteRows();
insertData("Morning", "2013-04-23 10:00:00", "Suresh Kumar");
insertData("Morning", "2013-04-23 11:30:00", "Pravat Das");
insertData("Evening", "2013-04-23 16:16:00", "Namita Roy");
insertData("Evening", "2013-04-23 17:30:00", "Rakesh Mitra");
insertData("After noon", "2013-04-23 14:27:00", "Anil Sarma");
// getTimeNameTimeAsc();
// getStudentNameTimeAsc();
// getTimeNameTimeDsc();
// getStudentNameTimeDsc();
// getTimeNameSyudentAsc();
// getStudentNameStudentAsc();
// getTimeNameStudentDsc();
// getStudentNameStudentDsc();
populateTableTimeAscending();
}
//==================================================================================//
//=============Functions for Table View=====================================================//
//==================================================================================//
//Time ascending method
public void populateTableTimeAscending()
{
getTimeNameTimeAsc();
getStudentNameTimeAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_asc.iterator();
Iterator itr2 = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Time descending method
public void populateTableTimeDescending()
{
getTimeNameTimeDsc();
getStudentNameTimeDsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click setOnClickListener---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
populateTableTimeAscending();
}
});
//----------------------On click setOnClickListener---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_time_dsc.iterator();
Iterator itr2 = student_name_list_time_dsc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Name ascending method
public void populateTableNameAscending()
{
getTimeNameSyudentAsc();
getStudentNameStudentAsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameDescending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_studname_asc.iterator();
Iterator itr2 = student_name_studname_asc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//Name descending method
public void populateTableNameDescending()
{
getTimeNameStudentDsc();
getStudentNameStudentDsc();
follow_up_table.removeAllViews();
//---------------Table Header-----------------------------------------------
TableRow followup_tr_head = new TableRow(this);
followup_tr_head.setId(10);
followup_tr_head.setBackgroundResource(R.drawable.list_header);
followup_tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView time_name_title = new TextView(this);
time_name_title.setId(20);
time_name_title.setText("Time");
time_name_title.setTextColor(Color.WHITE);
time_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(time_name_title);// add the column to the table row here
time_name_title.setTextSize(12);
TextView student_name_title = new TextView(this);
student_name_title.setId(20);
student_name_title.setText("Student Name");
student_name_title.setTextColor(Color.WHITE);
student_name_title.setPadding(5,5,5,5);
followup_tr_head.addView(student_name_title);// add the column to the table row here
student_name_title.setTextSize(12);
//----------------------On click time_name_title---------------------------------------
time_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableTimeDescending();
}
});
//----------------------On click time_name_title---------------------------------------
//----------------------On click student_name_title---------------------------------------
student_name_title.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//makeAToast("Hello!");
populateTableNameAscending();
}
});
//----------------------On click student_name_title---------------------------------------
follow_up_table.addView(followup_tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//--------------- Table Header-----------------------------------------------
Iterator itr = time_name_list_studname_dsc.iterator();
Iterator itr2 = student_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
while(itr2.hasNext())
{
//----------------table body------------------------------------------
followup_tr_data = new TableRow(this);
followup_tr_data.setId(10);
followup_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
followup_tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
final TextView timeNameText = new TextView(this);
timeNameText.setId(20);
timeNameText.setText(Html.fromHtml(itr.next().toString()));
timeNameText.setTextColor(Color.BLACK);
timeNameText.setPadding(5,5,5,5);
timeNameText.setTextSize(10);
followup_tr_data.addView(timeNameText);// add the column to the table row here
final TextView StudentNameText = new TextView(this);
StudentNameText.setId(20);
StudentNameText.setText(Html.fromHtml(itr2.next().toString()));
StudentNameText.setTextColor(Color.BLACK);
StudentNameText.setPadding(5,5,5,5);
StudentNameText.setTextSize(10);
followup_tr_data.addView(StudentNameText);// add the column to the table row here
follow_up_table.addView(followup_tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//----------------table body------------------------------------------
}
}
}
//==================================================================================//
//=============Functions for Table View=====================================================//
//==================================================================================//
//==================================================================================//
//=============Functions for DB=====================================================//
//==================================================================================//
//=================Time name ascending=================================
public void getTimeNameTimeAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_time_asc = db.getTimeNameTimeAsc();
Iterator itr = time_name_list_time_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameTimeAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_time_asc = db.getStudentNameTimeAsc();
Iterator itr = student_name_list_time_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Time name ascending=================================
//=================Time name descending=================================
public void getTimeNameTimeDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_time_dsc = db.getTimeNameTimeDsc();
Iterator itr = time_name_list_time_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameTimeDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_time_dsc = db.getStudentNameTimeDsc();
Iterator itr = student_name_list_time_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Time name descending=================================
//=================Student name ascending=================================
public void getTimeNameSyudentAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_studname_asc = db.getTimeNameSyudentAsc();
Iterator itr = time_name_list_studname_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameStudentAsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_studname_asc = db.getStudentNameStudentAsc();
Iterator itr = student_name_studname_asc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Student name ascending=================================
//=================Student name descending=================================
public void getTimeNameStudentDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
time_name_list_studname_dsc = db.getTimeNameStudentDsc();
Iterator itr = time_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
public void getStudentNameStudentDsc()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
student_name_list_studname_dsc = db.getStudentNameStudentDsc();
Iterator itr = student_name_list_studname_dsc.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
//=================Student name descending=================================
//method to insert data into local database
public void insertData(String timeName, String time, String studentName)
{
DatabaseHandler db = new DatabaseHandler(TableActivity.this);
ContentValues values = new ContentValues();
//db.createDataBase();
values.put("timeName",timeName);
values.put("time",time);
values.put("studentName",studentName);
db.insertValues(timeName, time, studentName);
db.close();
}
//method to delete all rows
public void deleteRows()
{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
db.deleteAllData();
db.close();
}
//==================================================================================//
//=============Functions for DB=====================================================//
//==================================================================================//
//method to show toast message
public void makeAToast(String str) {
//yet to implement
Toast toast = Toast.makeText(this,str, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
答案 0 :(得分:3)
您也可以使用:
textview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
textview.setTextColor(Color.BLUE);
break;
case MotionEvent.ACTION_UP:
textview.setTextColor(Color.BLACk);
break;
}
return false;
}
});
答案 1 :(得分:3)
点击时更改文字颜色的另一种方法是: 添加资源文件: 保存在res / color / button_text.xml的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
为你的widget设置为textColor:
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"
android:textColor="@color/button_text" />
的详细信息
答案 2 :(得分:1)
确保您尝试在正确的TextView中更改文本颜色,并尝试在populateTableTimeDescending()之后调用setTextColor;
答案 3 :(得分:0)
final TextView tv = new TextView(this);
tv.setText("Sample TextView");
rl = (RelativeLayout) findViewById(R.id.rl);
rl.addView(tv);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
populateTableTimeDescending();//since you are setting the color of the TextView back to same(white) again in this method, set the color of the textView after calling the method.
tv.setTextColor(Color.parseColor("#FF0000")); //**edited**
}
});
答案 4 :(得分:0)
您正尝试设置相同的颜色。看看吧。
请先用任何一种方法更改颜色。
--------------编辑-------------
然后在textview.setColor(Color.BLUE);
populateTableTimeDescending();
之前onClick();
答案 5 :(得分:0)
此处,在四位数之后,您可以使用十六进制颜色代码设置您想要的任何颜色:
time_name_title.setTextColor(0xffffffff);