[英]CodenameOne Text Field Focus on Android
我有一個文本字段“條形碼”和一個文本區域“條形碼列表”。
在啟動時,我想將焦點放在“條形碼”字段上。 我的 Android 掃描儀就像鍵盤一樣工作,並成功掃描到該字段。
當“條形碼”文本字段更改時,我想將內容添加到“條形碼列表”文本區域,然后將焦點設置回“條形碼”字段以准備下一次掃描。
問題是這在模擬器中有效,但在 Android 設備上無效。 在設備 (Unitech HT730) 上,掃描的數據被添加到“條形碼”字段,然后復制到“條形碼列表”,但焦點不會返回到“條形碼”字段。 閃爍的 cursor 出現在最右側的字段中,但我無法掃描(或輸入)到該字段中。 一旦我觸摸該字段,cursor 就會跳到最左邊,我可以再次掃描或輸入該字段。
我試過在調用 requestFocus() 之前插入 sleep(100)。 我也嘗試過使用 repaint()。 唯一有幫助的是刪除對 clear() 的調用。 似乎 clear() 必須弄亂除“條形碼”文本內容以外的其他內容,但我找不到任何其他方法來清除條形碼內容:即使 setText("") 也不起作用。
下面是我正在使用的代碼片段:
gui_Barcode_t.addDataChangeListener((i1, i2) -> {
String s1 = gui_Barcode_t.getText();
// A DataChange event will fire after the field is cleared, so test to see if this has occurred.
if (s1.length()> 0) {
List_s += s1 + "\n";
gui_BarcodeList_ta.requestFocus();
gui_BarcodeList_ta.setText(List_s);
sleep(100);
gui_BarcodeList_ta.repaint();
} else {
// This works in sim: repaint(), sleep(100), requestFocus()
gui_Barcode_t.repaint();
sleep(100);
gui_Barcode_t.requestFocus();
return;
}
// Note that a DataChange event will fire after the field is cleared.
// This works in sim: clear(), sleep(100), repaint(), sleep(100), requestFocus().
// This works on Android device if clear() is removed, but the text field is not cleared.
gui_Barcode_t.clear();
sleep(100);
gui_Barcode_t.repaint();
sleep(100);
gui_Barcode_t.requestFocus();
});
requestFocus()
非常適合焦點,但它可能會與本機編輯發生沖突,這是一個不同的過程。
您要使用的是startEditingAsync()
和stopEditing()
如果可能編輯了錯誤的字段。
作為旁注,除非您正在構建自定義組件,否則不應發出repaint()
調用。 你不應該在EDT上調用睡眠。 這是一個嚴重的錯誤!
謝謝,夏伊。 顯然我是 CNO 和移動 GUI 開發的新手。
除了在 startEditingAsync() 和 startEditingAsync() 中包裝 textedit 編輯之外,我還必須在 datachanged 事件處理程序之外處理這些編輯。
public void onBarcode_tDataChangeEvent(com.codename1.ui.Component cmp, int type, int index) {
BarcodeDataChangedFired = true;
}
task = new TimerTask() {
// run() method to carry out the action of the task
public void run() {
if (BarcodeDataChangedFired) {
BarcodeDataChangedFired = false;
String s1 = gui_Barcode_t.getText();
// If this event was fired because of the gui_Barcode_ta.setText(" ")..
if (s1.length() < 1) {
// Set focus back to the barcode field.
gui_Barcode_t.startEditingAsync();
//gui_BarcodeList_ta.stopEditing(); // THIS PREVENTS THE TEXTCHANGE FROM FIRING!
return;
}
// Play a sound to indicate that the barcode was read.
PlayMP3("bell4.wav");
// Copy the new barcode into the barcode list.
gui_BarcodeList_ta.startEditingAsync();
gui_BarcodeList_ta.setText(s1);
gui_BarcodeList_ta.stopEditing();
// Clear the barcode field.
gui_Barcode_t.startEditingAsync();
// This is the only way to clear the TextArea, and it will fire the DataChanged event.
gui_Barcode_t.clear();
gui_BarcodeList_ta.stopEditing();
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.