最近毕业设计也快做完了,因为也是边学Android边做毕设,而且也因为是初学,所以用了比较长时间,现在也是希望记下这段时间写Android的一些技巧方法或者是问题。

首先是关于EditText这个控件,这个控件用的也是非常普遍的,毕竟是程序用于和用户进行交互的一个重要控件。

1.取消EditText自动获取焦点的默认行为
一般在一进入有EditText的界面时,EditText就会自动获取焦点,但有时候我们并不希望EditText这种默认行为。

在网上搜了下,发现这种方法是有效的:
在EditText的父控件中加入这段代码:

android:focusable="true";
android:focusableInTouchMode="true";

这样就可以让EditText不会自动获取焦点了。
完整的xml代码如下:

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:focusable="true"
        android:focusableInTouchMode="true" >

        <EditText
            android:id="@+id/edit"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="搜索内容"
            android:singleLine="true"
            android:textColor="@android:color/black"
            android:textSize="20sp" />

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="搜索"
            android:textSize="20sp" />
    </LinearLayout>

2.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即可,如下图:
Android控件用法总结之EditText_获取焦点

只要修改成其他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() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }
        });

还有就是对搜索框内容的清除,参考自Android开发中的一个小功能 清空搜索框的文字

暂时总结到这,未完待续…