2016-07-26 7 views
0

AndroidでEditTextの線の色をプログラム的に変更する方法について、さまざまな回答がありました。 は、今私は、このソリューションを使用しています:Android:1つのEditTextの色合いを変更してください

final Drawable originalDrawable = editText.getBackground(); 
    final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable); 
    DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(darkVibrantColor)); 
    editText.setBackground(wrappedDrawable); 

は、これはのEditTextの色に、実際の変更でないが、残念ながらそれだけで私が使用している特定のEditTextの線の色が、線の色を変更しません。私のアプリケーションで使用されるすべてのEditTextsのそれらが異なる活動にある場合のイベント。

グローバルに線の色を変更せずに特定のEditTextの線の色を変更するにはどうすればよいですか?ありがとう。

アップデート:アプリケーションの実行中に、色が動的に生成されるため、あらかじめ定義されたスタイルを使用することはできません。

+0

あなたはstackoverflowの – SaravInfern

+0

を参照してください[この回答](http://stackoverflow.com/a/34004110/5744335)のスタイルを作成することができますスタイルを使用するのではなく、プログラム的に色を変更する必要があります。私は質問を更新しました。 – Arshak

+0

私に与えられたあなたのEditTextビュー – Androidicus

答えて

0

の下線の色を変更するあなたのスタイル

<EditText 
    style="@style/MyEditText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:hint="Subject"/> 
+0

ありがとうございます。残念ながら、アプリの実行中は画像から色が生成されるため、プログラムで色を変更する必要があります。質問が更新されました。 – Androidicus

0

を追加EDITTEXT私はプログラム的にコードを提供してきたあなたにあなたのstyle.xmlであなたに

<style name="MyEditText" parent="Theme.AppCompat.Light"> 
    <item name="colorControlNormal">@color/indigo</item> 
    <item name="colorControlActivated">@color/pink</item> 
    <item name="android:padding">20dp</item> 
    <item name="android:textSize">16dp> 
    <item name="android:editTextColor">@color/black</item> 
    <item name="android:tint">@color/colorPrimary</item> 
</style> 

を助けることができる、これを試してみてくださいEditText

EditText editTextOne=(EditText) findViewById(R.id.edit_text_one); 
Drawable drawable=editTextOne.getBackground(); 
int color=Color.parseColor("#FFFFFF"); 
drawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP); 
if (Build.VERSION.SDK_INT > 16) { 
    editTextOne.setBackground(drawable); 
} else { 
    editTextOne.setCompoundDrawables(null,null,drawable,null); 
} 
+0

ありがとうございます。残念ながらそれはどちらもうまくいきませんでした。 EditTextは色を変更しますが、異なるアクティビティのEditTextも色が変わります。 – Androidicus

+0

私はこのコードをテストしましたが、他のアクティビティのEditTextの下線色は変更しません。カスタムEditTextを使用していますか? @Androidicus – Arshak

+0

@Arhsakいいえ私は標準のEditText実装を使用しています。私が下に投稿したように、ドロウアブルを変更することができました。なぜあなたのソリューションが私のために働かなかったのか分かりません。とにかく、ありがとう。 – Androidicus

0

解決策が見つかりました。 this(not accepted)という回答に掲載されています。そのトリックは、それがEditTextから検索された後に、バックグラウンド描画可能オブジェクトを変更することです。

は、ここに私の作業コードです:

final Drawable originalDrawable = editText.getBackground(); 
originalDrawable.mutate(); 
final Drawable wrappedDrawable = DrawableCompat.wrap(originalDrawable); 
DrawableCompat.setTintList(wrappedDrawable, ColorStateList.valueOf(darkVibrantColor)); 
editText.setBackground(wrappedDrawable); 
関連する問題