0

我有一个活动,我在其中动态创建了一个表。该表是从 SQLite 数据库填充的。Onclick 表格标题,表格将得到相应的排序。

一切正常,现在我的要求是在单击时更改表头文本视图的文本颜色。文本颜色没有改变。

是填充表的方法

//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();
    }
}
4

6 回答 6

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;
            }
        });
于 2013-04-25T09:34:24.720 回答
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>

并为您的小部件设置为 textColor:

<Button
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/button_text"
    android:textColor="@color/button_text" />

有关更多信息,请查看谷歌指南

于 2013-05-31T12:22:08.337 回答
1

确保您尝试在正确的 TextView 中更改文本颜色,并尝试在 populateTableTimeDescending(); 之后调用 setTextColor;

于 2013-04-25T06:45:21.667 回答
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**
        }
    });
于 2013-04-25T06:48:39.757 回答
0

您正在尝试设置相同的颜色。看看这个。在此处输入图像描述

在此处输入图像描述

请在任何方法中先更改颜色。

- - - - - - - 编辑 - - - - - - -

然后在你textview.setColor(Color.BLUE);之前做populateTableTimeDescending();onClick();

于 2013-04-25T07:39:49.027 回答
0

在这里,在四位数字之后,您可以使用十六进制颜色代码设置您想要的任何颜色:

time_name_title.setTextColor(0xffffffff);
于 2013-04-25T11:48:06.127 回答