Android : on changing EditText background color, EditText 样式改变

标签 android android-layout

您好,当我更改两个 EditTextEditText 的背景颜色时,它们看起来像是合并了。

我的布局代码是这样的`

<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" />

`

我的 Activity 代码是这样的

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

最佳答案

如你所见here使用 EditText.setBackgroundColor(any color) 也会为您的轮廓着色。为了保持大纲方面,我建议将编辑文本包含在表格行中并使用边距。在您的 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>

关于Android : on changing EditText background color, EditText 样式改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11840610/

相关文章:

java - Android - 在提交之前(期间)检查 FragmentManager 事务是否存在由 fragment 代码引起的异常

android - 如何扩展 Android 按钮并使用 xml 布局文件

android - 首次应用安装时间

android - 在 onCreate() 中生成 main.xml

Android - layout_weight 使图像在两侧可点击

android - 使用 IBM Mobile First Platform 生成 APK

android - 通过单击按钮停止 geocomplete 在输入字段中添加用户定义的位置

android - 为 fragment 设置新布局

android - 如何在按钮单击时显示/隐藏布局

android - 设置 Android 编辑文本的样式,使其看起来像下拉/微调器选择器