簡體   English   中英

對話框關閉后的EditText光標bug

[英]EditText cursor bug after dialog close

錯誤演示

我遇到了問題。 不知道該怎么稱呼它,或者是什么原因引起它

我正在學習Android SQLite並開始編寫一個簡單的筆記應用程序。

問題是我有一個類別選擇的自定義對話框,在打開對話框之前,EditText字段中的一切都很好,但打開后關閉它,文本開始寫入,就像創建相同文本的多個圖層一樣,文本光標離開每個符號后面的一行。 (參見“bug演示”問題的GIF)

有沒有人見過這樣的東西? 什么可能導致這個,對話?

編輯:

因此,這是在單擊星形以打開對話框時采取操作的代碼

 starred.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(CreateNoteActivity.this);
                View mView = getLayoutInflater().inflate(R.layout.dialog_category_select, null);
                ListView categoryList = mView.findViewById(R.id.category_list);
                Button cancelSelect = mView.findViewById(R.id.cancelSelect);

                final CategoryListAdapter adapter = new CategoryListAdapter(CreateNoteActivity.this);
                categoryList.setAdapter(adapter);
                //get the data and append to a list
                Cursor data = myDB.getCategories();
                while(data.moveToNext()){
                    Category thisNote = new Category(data.getInt(0), data.getString(1), data.getString(2));
                    adapter.add(thisNote);
                }

                categoryList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, final int i, long l) {
                        final Category selectedCategory = (Category) adapterView.getItemAtPosition(i);
                        int duration = Toast.LENGTH_SHORT;
                        String s = "Category celected: "+selectedCategory.getCategoryName();
                        Toast toast = Toast.makeText(context, s, duration);
                        toast.show();
                    }
                });

                builder.setView(mView);

                final AlertDialog selectCategory = builder.create();
                selectCategory.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));
                selectCategory.show();
                View decorView = getWindow().getDecorView();
                decorView.setBackgroundResource(android.R.color.transparent);
                int width = (int)(getResources().getDisplayMetrics().widthPixels*0.80);
                int height = (int)(getResources().getDisplayMetrics().heightPixels*0.80);

                selectCategory.getWindow().setLayout(width, height);

                cancelSelect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        selectCategory.dismiss();
                    }
                });

        }
    });

這個答案可能對你有幫助

關閉對話框后寫下此內容

ediText = findViewById(R.id.edit_text);

editText.setSelection(editText.getText().length);

基本上使用上面的邏輯,光標不會指向dialog關閉時editText的第一個字符

注意到我試圖設置透明背景以顯示我的自定義對話框bcg兩次。 所以解決問題的方法是刪除兩行

*View decorView = getWindow().getDecorView();
decorView.setBackgroundResource(android.R.color.transparent);*

不知道為什么會造成這種情況。 應檢查什么是getDecorView()方法。 使用它導致它作為顯示自定義背景的解決方案。

這條線也適用

dialog.getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent)));

猜猜這是橡皮鴨調試的情況 - 只需要告訴別人有關修復它的問題。 感謝大家。

關閉或關閉對話框時嘗試使用此代碼,

edittext.setSelection(editText.getText().toString().trim().length);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM