日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android控件用法总结之EditText

發布時間:2023/12/10 Android 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android控件用法总结之EditText 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最近畢業設計也快做完了,因為也是邊學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即可,如下圖:

只要修改成其他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開發中的一個小功能 清空搜索框的文字

暫時總結到這,未完待續…

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Android控件用法总结之EditText的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。