textColor和textAppearance of edittext

时间:2013-08-19 03:41:11

标签: android android-edittext textcolor

这是我的代码

<style name="AppBaseTheme" parent="android:Theme.Light">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:textAppearance">@style/App_TextAppearance</item>
    <item name="android:editTextStyle">@style/App_EditTextStyle</item>
    <item name="android:textViewStyle">@style/App_TextViewStyle</item>
</style>

<style name="App_TextAppearance" parent="@android:style/TextAppearance">
    <item name="android:textColor">#ffff0000</item>
</style>

<style name="App_EditTextStyle" parent="@android:style/Widget.EditText">
    <item name="android:textAppearance">?android:textAppearance</item>
</style>

<style name="App_TextViewStyle" parent="@android:style/Widget.TextView">
    <item name="android:textAppearance">?android:textAppearance</item>
</style>

问题是,现在所有textview的文本颜色都是RED,但所有edittexts的文本颜色仍然是黑色。我知道我需要将一个android:textColor属性项设置为App_EditTextStyle以更改edittext的颜色,但为什么呢? android:textAppearance attr对edittext没用吗?

1 个答案:

答案 0 :(得分:1)

我在代码中尝试过这种更改,而不是在xml部分。可以使用EditText的onFocusChangeListener来实现。

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;

public class MainActivity extends Activity {
/** Called when the activity is first created. */

EditText et1;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    et1 = (EditText) findViewById(R.id.editText1);        


    et1.setOnFocusChangeListener(new OnFocusChangeListener() {

        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus)  //Whenever your EditText is focused this color will be applied
                  et1.setTextColor(Color.RED);           
              else //Otherwise display the text in Gray Color
                  et1.setTextColor(Color.GRAY);             
        }
    });

}
}

这个对我有用。希望它对你也有帮助。