Android中EditText setText方法的踩坑實(shí)戰(zhàn)
1、平平常常中就這樣開始
某一天,我準(zhǔn)備做一個(gè)搜索功能,這個(gè)搜索功能呢大概是在主活動(dòng)A中,用EditText接收輸入,當(dāng)EditText監(jiān)聽到輸入框中內(nèi)容有變化,跳轉(zhuǎn)到活動(dòng)B中,活動(dòng)B中準(zhǔn)備有搜索歷史記錄等等,等在活動(dòng)B中確定好搜索關(guān)鍵詞后,跳回到活動(dòng)A中,執(zhí)行搜索,并顯示搜索結(jié)果……一切順順利利,然后呢,懵逼了,我回不了活動(dòng)A了。
當(dāng)時(shí)的情況大致是這樣的,
布局文件:activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".ActivityA"> <EditText android:inputType="text" android:singleLine="true" android:imeOptions="actionSearch" android:id="@+id/et_search" android:textSize="24sp" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入點(diǎn)啥唄" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:textSize="24sp" android:gravity="center" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="我是主活動(dòng)啦"/> </LinearLayout>
活動(dòng)A:AcitivityA.java
public class ActivityA extends AppCompatActivity { private EditText searchEditText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //找到EditText,添加文本監(jiān)聽 searchEditText=findViewById(R.id.et_search); searchEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d("editTextSetText","beforeTextChanged"); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("editTextSetText","onTextChanged"); } @Override public void afterTextChanged(Editable s) { Log.d("editTextSetText","afterTextChanged"); startActivity(new Intent(ActivityA.this,ActivityB.class)); } }); //接收B活動(dòng)傳遞過來的keywords,并顯示在輸入框中 String keyword=getIntent().getStringExtra("keywords"); if(keyword!=null) searchEditText.setText(keyword); }
活動(dòng)B布局文件:activity_search.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <EditText android:id="@+id/et_search_keywords" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="輸入你想要找的東西" android:textSize="24sp" android:inputType="text" android:singleLine="true" android:imeOptions="actionSearch"/> <TextView android:textSize="24sp" android:gravity="center" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="我就是那個(gè)準(zhǔn)備搜索關(guān)鍵詞,歷史記錄等等等等的活動(dòng)B啦" /> </LinearLayout>
活動(dòng)B:ActivityB.java
public class ActivityB extends AppCompatActivity { private EditText editTextKeyWords; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); editTextKeyWords = findViewById(R.id.et_search_keywords); editTextKeyWords.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { //將取得關(guān)鍵字傳遞到A中 String keywords = v.getText().toString().trim(); Intent intent = new Intent(ActivityB.this, ActivityA.class); intent.putExtra("keywords", keywords); Log.d("editTextSetText", "B keywords:" + keywords); startActivity(intent); ActivityB.this.finish(); } return false; } }); } }
OK,至此大功告成,我成功復(fù)原了當(dāng)時(shí)我大致做法,也還原了當(dāng)時(shí)的Bug:在A中有輸入時(shí),跳到B,在B中確定好關(guān)鍵詞后,點(diǎn)擊搜索(你的搜索在哪里,讓你們看下圖吧)
看到右下角我圈起來的那個(gè)搜索按鈕了嗎,布局文件照著我那個(gè)寫,監(jiān)聽就是editTextKeyWords.setOnEditorActionListener(……),布局中重要的是
android:inputType="text" android:singleLine="true" android:imeOptions="actionSearch"
這個(gè)主要是參看這位大大的,當(dāng)然了,在此這不算重點(diǎn),重點(diǎn)是我又成功寫了個(gè)Bug。當(dāng)時(shí)項(xiàng)目挺急的,我弄了半天沒弄明白,(我一直以為是A活動(dòng)因?yàn)閟etText而崩潰了,然而沒有l(wèi)og……當(dāng)然,最終證明似乎不是這樣子。),無奈之下,福靈心至,想到:
2、用了setHint()解決了當(dāng)務(wù)之急
無奈之下選擇了該方法,問題成功解決,也沒什么明顯瑕疵,就是心理一直惦記著,這他丫的問題出在什么地方呢,定位當(dāng)然是定位到了A活動(dòng)中的searchEditText.setText(keyword)
這一句。
后來加了幾天班,等稍微有空了,我再回頭瞧瞧,莫非,谷歌給我們寫了個(gè)小bug,結(jié)果嘛:事實(shí)證明,好像這東西是自己的鍋。
3、發(fā)現(xiàn)敵蹤跡
當(dāng)時(shí)我實(shí)在onResume()
調(diào)用searchEditText.setText(keyword)
這幾句的,所以問題顯得比我上面寫的要隱蔽些。當(dāng)然,有空了之后,我進(jìn)入setText方法,一步步執(zhí)行,當(dāng)然了,沒發(fā)現(xiàn)明顯問題。只是我偶爾會(huì)發(fā)現(xiàn)它會(huì)不經(jīng)意間往beforeTextChanged等這一兩個(gè)方法中眺一下,那么想一想我在里面又做了什么……,似乎我發(fā)現(xiàn)問題所在了。那么有了猜測(cè),對(duì)該方法添加log,我們隊(duì)log做下修改,使其能明確地顯示程序執(zhí)行順序,修改代碼如下:
ActivityA.java
public class ActivityA extends AppCompatActivity { private EditText searchEditText; public static int executeOrder=0;//表示log執(zhí)行順序,進(jìn)而推測(cè)代碼執(zhí)行順序 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.d("editTextSetText","after setContentView "+executeOrder++); //找到EditText,添加文本監(jiān)聽 searchEditText=findViewById(R.id.et_search); searchEditText.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { Log.d("editTextSetText","beforeTextChanged "+executeOrder++); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { Log.d("editTextSetText","onTextChanged "+executeOrder++); } @Override public void afterTextChanged(Editable s) { Log.d("editTextSetText","afterTextChanged "+executeOrder++); startActivity(new Intent(ActivityA.this,ActivityB.class)); } }); //接收B活動(dòng)傳遞過來的keywords,并顯示在輸入框中 String keyword=getIntent().getStringExtra("keywords"); if(keyword!=null) { searchEditText.setText(keyword); Log.d("editTextSetText","after set text "+executeOrder++); /*searchEditText.setHint(keyword);*/ } } }
ActivityB.java
public class ActivityB extends AppCompatActivity { private EditText editTextKeyWords; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); Log.d("editTextSetText", "B after setContentView " +ActivityA.executeOrder++); editTextKeyWords = findViewById(R.id.et_search_keywords); editTextKeyWords.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_SEARCH) { //將取得關(guān)鍵字傳遞到A中 String keywords = v.getText().toString().trim(); Intent intent = new Intent(ActivityB.this, ActivityA.class); intent.putExtra("keywords", keywords); Log.d("editTextSetText", "B keywords:" + keywords+" " +ActivityA.executeOrder++); startActivity(intent); ActivityB.this.finish(); } return false; } }); } }
執(zhí)行一遍,log如下:
上述log顯示,做如下解讀:
1、 啟動(dòng)應(yīng)用,執(zhí)行初始化,打印 0
2、輸入內(nèi)容,執(zhí)行1,2,3,到啟動(dòng)活動(dòng)B
3、活動(dòng)B初始化執(zhí)行 4
5、 活動(dòng)B中點(diǎn)擊搜索 5,并啟動(dòng)活動(dòng)A
6、A再一次初始化 執(zhí)行6
7、問題出來了,為什么會(huì)執(zhí)行7、8、9,此時(shí)我們并沒有EditText輸入內(nèi)容,但是監(jiān)聽觸發(fā)了。
8、 執(zhí)行9之后,啟動(dòng)活動(dòng)B,執(zhí)行11沒問題,但是10的順序按理說應(yīng)該在6之后、緊接著6.
盡管未能完全解讀這個(gè)執(zhí)行順序,但是,寫的程序陷入了一個(gè)類似死循環(huán)的bug是沒有問題的,這也就解開了為什么返回不了A活動(dòng)的問題,并不是不能返回A,而是返回A之后又跳轉(zhuǎn)到B了。
進(jìn)一步調(diào)試,在關(guān)鍵節(jié)點(diǎn)增加log,我們?cè)賡etText前后增加log
if(keyword!=null) { Log.d("editTextSetText","after set text "+executeOrder++); searchEditText.setText(keyword); Log.d("editTextSetText","after set text "+executeOrder++); /*searchEditText.setHint(keyword);*/ }
log信息:
關(guān)鍵點(diǎn)我已經(jīng)標(biāo)出來了,在7和11間,有了監(jiān)聽方法的執(zhí)行,說明:EditText的setText方法會(huì)觸發(fā) 文本變化的監(jiān)聽,這就是此次踩坑的根源。
4、 捕獲兇手:setText()
方法會(huì)觸發(fā)EditText文本變化的監(jiān)聽
5、解決方案:將setText設(shè)置在設(shè)置監(jiān)聽之前,或者用setHint也可以。
如上,找出問題癥結(jié)之后,解決辦法就很簡(jiǎn)單了,我們將setText寫在設(shè)置監(jiān)聽之前就可以避免該坑,或者干脆用setHint方法。
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- Android中ViewPager組件的基本用法及實(shí)現(xiàn)圖片切換的示例
- android Gallery組件實(shí)現(xiàn)的iPhone圖片滑動(dòng)效果實(shí)例
- Android組件Glide實(shí)現(xiàn)圖片平滑滾動(dòng)效果
- Android自定義組件獲取本地圖片和相機(jī)拍照?qǐng)D片
- Android可循環(huán)顯示圖像的Android Gallery組件用法實(shí)例
- Android高級(jí)組件Gallery畫廊視圖使用方法詳解
- Android高級(jí)組件ImageSwitcher圖像切換器使用方法詳解
- 淺析Android Studio 3.0 升級(jí)各種坑(推薦)
- Android WebView使用的技巧與一些坑
- Android開發(fā)之StackView用法和遇到的坑分析
相關(guān)文章
Cocos2d-x入門教程(詳細(xì)的實(shí)例和講解)
這篇文章主要介紹了Cocos2d-x入門教程,包括詳細(xì)的實(shí)例、講解以及實(shí)現(xiàn)過程,需要的朋友可以參考下2014-04-04詳解Android中的MVP架構(gòu)分解和實(shí)現(xiàn)
本篇文章主要介紹了詳解Android中的MVP架構(gòu)分解和實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-02-02C/C++在Java、Android和Objective-C三大平臺(tái)下實(shí)現(xiàn)混合編程
本文主要介紹C/C++在Java、Android和Objective-C三大平臺(tái)下實(shí)現(xiàn)混合編程,這里舉例說明實(shí)現(xiàn)不同平臺(tái)用C/C++實(shí)現(xiàn)編程的方法,有興趣的小伙伴可以參考下2016-08-08關(guān)于Android中WebView遠(yuǎn)程代碼執(zhí)行漏洞淺析
這篇文章主要給大家介紹了關(guān)于Android中WebView遠(yuǎn)程代碼執(zhí)行漏洞的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-05-05深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解
這篇文章主要介紹了深入Android中BroadcastReceiver的兩種注冊(cè)方式(靜態(tài)和動(dòng)態(tài))詳解,具有一定的參考價(jià)值,有需要的可以了解一下。2016-12-12Android編程解析Json格式數(shù)據(jù)的方法
這篇文章主要介紹了Android編程解析Json格式數(shù)據(jù)的方法,涉及Android中json格式數(shù)據(jù)的構(gòu)造、讀取及遍歷等技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-11-11Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)將View轉(zhuǎn)化為圖片并保存到本地,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
這篇文章主要介紹了Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07