2015-11-03 52 views
1

我在我的应用中使用了编辑文本,并且我想在编辑文本聚焦时更改背景。我写了一些代码,但我面临一个问题。我需要两次点击编辑文本才能显示键盘。android编辑文本更改焦点背景

这是我的代码:

private View.OnFocusChangeListener myEditTextFocus = new View.OnFocusChangeListener() { 
    public void onFocusChange(View view, boolean hasfocus) { 
     if (hasfocus) { 
      ((EditText) view).setBackgroundResource(R.drawable.edittext_input_background_focus); 

      ((EditText) view).setTextColor(Color.parseColor("#4d4d4d")); 

     } 
     else { 
      ((EditText) view).setBackgroundResource(R.drawable.edittext_input_background_not_focus); 

     } 
    }; 
}; 

问题是这样的代码,因为我评论它,一切都棒极是错我的代码?或者,还有其他解决方案吗?

+0

你试过在绘制? –

+0

不,我不知道如何使用它。我想要chenage textcolor和背景焦点@ChiragSavsani Savsani – donoachua

回答

3

//按照此代码

把这些3个绘制文件RES /绘制的文件夹

simple_edittext.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:padding="10dp" 
    android:shape="rectangle" > 

    <solid android:color="#FFFFFF" /> 

    <stroke 
     android:width="3dp" 
     android:color="#FB9820" /> 

    <corners 
     android:bottomLeftRadius="5dp" 
     android:bottomRightRadius="5dp" 
     android:topLeftRadius="5dp" 
     android:topRightRadius="5dp" /> 

</shape> 

focus_edittext.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:padding="10dp" 
    android:shape="rectangle" > 
    <solid android:color="#58ACFA" /> 

    <stroke 
     android:width="3dp" 
     android:color="#FB9820" /> 
    <corners 
     android:bottomLeftRadius="5dp" 
     android:bottomRightRadius="5dp" 
     android:topLeftRadius="5dp" 
     android:topRightRadius="5dp" /> 
</shape> 

selector_edittext.xml

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="true" android:drawable="@drawable/focus_edittext"/> 
    <item android:drawable="@drawable/simple_edittext" /> 
</selector> 

,并使用这些drawbles这样的:

<EditText 
     android:id="@+id/layout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:padding="10dp" 
     android:layout_marginBottom="10dp" 
     android:background="@drawable/selector_edittext" 
     android:text="@string/hello_world" /> 

    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/layout1" 
     android:padding="10dp" 
     android:background="@drawable/selector_edittext" 
     android:text="@string/hello_world" /> 
相关问题