android开发编辑框样式,Android -- 编辑框更改样式

importandroid.app.Activity;importandroid.content.res.Resources;importandroid.graphics.drawable.Drawable;importandroid.os.Bundle;importandroid.text.Editable;importandroid.text.InputType;importandroid.text.TextUtils;importandroid.text.TextWatcher;importandroid.view.Menu;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.View.OnTouchListener;importandroid.widget.EditText;importandroid.widget.Toast;public class MainActivity extendsActivity {private Drawable mIconSearchDefault; //搜索文本框默认图标

private Drawable mIconSearchClear; //搜索文本框清除文本内容图标

private EditText mSearchView = null;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);//得到资源里面的图标文件

final Resources res =getResources();//默认的图标

mIconSearchDefault =res.getDrawable(R.drawable.txt_search_default);//清除图标

mIconSearchClear =res.getDrawable(R.drawable.txt_search_clear);

mSearchView=(EditText) findViewById(R.id.txtSearch);

mSearchView.addTextChangedListener(tbxSearch_TextChanged);

mSearchView.setOnTouchListener(txtSearch_OnTouch);

}

@Overridepublic booleanonCreateOptionsMenu(Menu menu) {//Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);return true;

}/*** 判断输入框中是否有数据,然后显示相应的图标文件*/

private TextWatcher tbxSearch_TextChanged = newTextWatcher() {//缓存上一次文本框内是否为空

private boolean isnull = true;

@Overridepublic voidafterTextChanged(Editable s) {if(TextUtils.isEmpty(s)) {if (!isnull) {

mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,null, mIconSearchDefault, null);

isnull= true;

}

}else{if(isnull) {

mSearchView.setCompoundDrawablesWithIntrinsicBounds(null,null, mIconSearchClear, null);

isnull= false;

}

}

}

@Overridepublic void beforeTextChanged(CharSequence s, int start, intcount,intafter) {

}/*** 随着文本框内容改变动态改变列表内容*/@Overridepublic void onTextChanged(CharSequence s, int start, intbefore,intcount) {

}

};//当清除图标被点击的时候的处理事件

private OnTouchListener txtSearch_OnTouch = newOnTouchListener() {

@Overridepublic booleanonTouch(View v, MotionEvent event) {switch(event.getAction()) {caseMotionEvent.ACTION_UP:int curX = (int) event.getX();if (curX > v.getWidth() - 38

&& !TextUtils.isEmpty(mSearchView.getText())) {

mSearchView.setText("");int cacheInputType = mSearchView.getInputType();//backup//the input//type

mSearchView.setInputType(InputType.TYPE_NULL);//disable//soft//input

mSearchView.onTouchEvent(event);//call native handler

mSearchView.setInputType(cacheInputType);//restore input

Toast toast = Toast.makeText(MainActivity.this, "你好啊", Toast.LENGTH_SHORT);

toast.show();//type

return true;//consume touch even

}break;

}return false;

}

};

}

Android中,如果你想改变EditText编辑框的下边框颜色,你可以通过自定义视图或者主题来自定义样式。这里提供两种常见的方法: 1. **自定义样式**: - 创建一个新的XML文件(如`custom_edittext_style.xml`),在其中设置`<shape>`标签,并添加`<stroke>`元素来指定边框颜色和宽度: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="@android:color/white" /> <!-- 或者你喜欢的颜色 --> <padding android:bottom="1dp" /> <stroke android:width="1px" android:color="@color/custom_border_color" /> <!-- 替换为你的颜色ID --> <corners android:radius="4dp" /> <!-- 可选,设置圆角 --> </shape> ``` - 在你的布局文件中,将`android:background`属性引用这个自定义样式: ```xml <EditText android:id="@+id/your_edit_text_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/custom_edittext_style" /> ``` 2. **修改应用主题**: - 如果你想在整个应用中更改所有EditText的下边框颜色,可以在`styles.xml`文件中创建一个新的主题,然后覆盖默认样式: ```xml <style name="AppTheme.EditTextBorder"> <item name="editTextBackground">@drawable/custom_edittext_shape</item> <!-- 引用上面的自定义形状资源 --> </style> ``` - 然后在AndroidManifest.xml中设置应用的主题: ```xml <application android:name=".YourAppName" ...> <style name="YourAppName.Theme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- 将下面这行改为你的主题名 --> <item name="android:editTextStyle">@style/AppTheme.EditTextBorder</item> </style> ... </application> ``` 记得替换`custom_border_color`为你想要的颜色值或者颜色资源ID。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值