湖南长沙网站建,试论述网上商城的推广技巧,跨境电商如何做,濮阳网站建设 公司名字最近毕业设计也快做完了#xff0c;因为也是边学Android边做毕设#xff0c;而且也因为是初学#xff0c;所以用了比较长时间#xff0c;现在也是希望记下这段时间写Android的一些技巧方法或者是问题。
首先是关于EditText这个控件#xff0c;这个控件用的也是非常普遍的…最近毕业设计也快做完了因为也是边学Android边做毕设而且也因为是初学所以用了比较长时间现在也是希望记下这段时间写Android的一些技巧方法或者是问题。
首先是关于EditText这个控件这个控件用的也是非常普遍的毕竟是程序用于和用户进行交互的一个重要控件。
1.取消EditText自动获取焦点的默认行为 一般在一进入有EditText的界面时EditText就会自动获取焦点但有时候我们并不希望EditText这种默认行为。
在网上搜了下发现这种方法是有效的 在EditText的父控件中加入这段代码
android:focusabletrue;
android:focusableInTouchModetrue;
这样就可以让EditText不会自动获取焦点了。 完整的xml代码如下 LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationhorizontalandroid:focusabletrueandroid:focusableInTouchModetrue EditTextandroid:idid/editandroid:layout_width0dpandroid:layout_heightwrap_contentandroid:layout_weight1android:hint搜索内容android:singleLinetrueandroid:textColorandroid:color/blackandroid:textSize20sp /Buttonandroid:idid/buttonandroid:layout_widthwrap_contentandroid:layout_heightwrap_contentandroid:gravitycenterandroid:text搜索android:textSize20sp //LinearLayout2.Eclipse 在xml布局文件中一旦采用了EditText控件后可视化视图中就会出现 Exception raised during rendering:java.lang.System.arraycopy([CI[CII)V
Exception details are logged in Window Show View Error Log
这个问题并且也无法看到布局只能在模拟器上看但每次仅仅修改下布局就要启动模拟器来查看布局会非常不方便于是也上网查了下发现其实只要修改下不同的API即可如下图
只要修改成其他API就可以显示。
3.关于AlertDialog中自定义布局带有的EditText无法弹出键盘 这个之前总结过了Android学习问题关于AlertDialog中自定义布局带有的EditText无法弹出键盘
4.关于选择部分或全选文本以及光标位置问题
1选择部分文本和全选
EditText txt (EditText) findViewById(R.id.edit);txt.setText(hello!);//txt.setSelection(0, 3);//此方法等同于下面Selection类的方法Selection.setSelection(txt.getEditableText(), 0,3);//全选txt.selectAll();
2.光标位置的设置
//设置光标位置在最后的位置
txt.setSelection(txt.length());
//Selection.setSelection(etSelection.getEditableText(), 3);//设置光标在第三个字符后面
所以这里分别用了两种方式一种是直接通过EditText的setSelection方法另一种则是采用Selection类的setSelection的方法这两种方法的具体定义如下
void android.widget.EditText.setSelection(int start, int stop)
public void setSelection (int start, int stop) void android.text.Selection.setSelection(Spannable text, int start, int stop)
public static void setSelection (Spannable text, int start, int stop)
start:表示选择文本开始的位置 stop:表示选择文本结束的位置实际上选择的文本数就是stop-start也可以说是索引值从start—stop-1的范围。
而两种方式的setSelection方法在只有一个int索引值时就是表示设置光标的位置其int参数就是光标偏离值。
5.对EditText控件内容的监听方法
EditText txt (EditText) findViewById(R.id.edit);
txt.addTextChangedListener(new TextWatcher() {Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {// TODO Auto-generated method stub}Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {// TODO Auto-generated method stub}Overridepublic void afterTextChanged(Editable s) {// TODO Auto-generated method stub}});
还有就是对搜索框内容的清除参考自Android开发中的一个小功能 清空搜索框的文字
暂时总结到这未完待续…