安卓:更改EditText背景颜色后,EditText的样式也会发生变化。

4

你好,当我改变两个 EditText 的背景颜色时,它们看起来像是合并在一起。

我的布局代码如下:

<EditText
    android:id="@+id/editText4"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Text1"
    android:singleLine="true" />

<EditText
    android:id="@+id/editText1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Text2"
    android:singleLine="true" >
</EditText>

<AutoCompleteTextView android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:imeOptions="actionNext"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Text3"
        android:singleLine="true" />

</LinearLayout>
 <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <EditText
        android:id="@+id/editText2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:hint="Text4"
        android:singleLine="true" />

</LinearLayout>

 <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Button" />

并且我的活动代码如下所示
public class MainActivity extends Activity {

EditText editText1, editText2, editText3, editText4;
Button button;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();

    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            editText1.setBackgroundColor(getResources().getColor(android.R.color.primary_text_dark_nodisable));
            editText3.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));

        }
    });

}

private void init() {
    editText1 = (EditText) findViewById(R.id.editText1);
    editText2 = (EditText) findViewById(R.id.editText2);
    editText3 = (EditText) findViewById(R.id.editText4);
    button = (Button) findViewById(R.id.button1);

}

请参考下面附加的屏幕截图:

点击按钮前的布局:

enter image description here

点击按钮后的布局:

enter image description here


你试过其他两种颜色来看看区别吗?我没有看到它们合并。我只看到边框不再可见了。 - cosmincalistru
感谢@cosmincalistru的评论,是的,它们实际上并没有合并,但在视图中它们看起来像是合并了。所有颜色都有相同的效果。是否有任何解决方案? - Sharanabasu Angadi
2个回答

3

正如您在这里所看到的,使用EditText.setBackgroundColor(any color)会同时着色您的轮廓线。为了保持轮廓线的外观,我建议将EditText包含在表行中并使用边距。尝试在您的xml中使用以下代码并查看结果:

<TableRow
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@android:color/black"
    android:orientation="vertical" >

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_orange_dark"
        android:layout_margin="1dip"
        android:text="cosmincalistru" />
</TableRow>

感谢 @cosmincalistru 的回答,我尝试使用 TableLayout 仍然得到相同的结果。 - Sharanabasu Angadi
很高兴能帮上忙。无论如何,好问题,今天发现了新东西。 - cosmincalistru

-1

你为两个EditText设置了相同的id。@+id/editText2

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Text3"
    android:singleLine="true" />

<EditText
    android:id="@+id/editText2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Text4"
    android:singleLine="true" />


谢谢你的建议,Nick。但问题与那个无关,我尝试进行了更正,但结果仍然相同。 - Sharanabasu Angadi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接