简体   繁体   中英

Android EditText doesn't show text

I have 3 edittext's in userdialog, setting them dynamically. 2 of them typing text properly, but 1 doesn't on some devices (android 2.3.4). Already tried solution from EditText freezes/doesn't show text whilst typing .

Here's my code:

    AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
    alertDialog.setTitle("Имя устройства");
    LinearLayout layout = new LinearLayout(context);


    //alertDialog.setMessage("Задайте имя устройства");
    final EditText inputName = new EditText(MainActivity.this); 
    inputName.setInputType(InputType.TYPE_CLASS_TEXT);
    inputName.setOnEditorActionListener(doneListener);

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                          LinearLayout.LayoutParams.MATCH_PARENT,
                          LinearLayout.LayoutParams.MATCH_PARENT);
    layout.setOrientation(LinearLayout.VERTICAL);
    lp.setMargins(20, 20, 20, 20);
    layout.setLayoutParams(lp);


    final EditText inputInv = new EditText(MainActivity.this);
    inputInv.setInputType(InputType.TYPE_CLASS_NUMBER);
    inputInv.setTextColor(Color.BLACK);
    inputInv.setOnEditorActionListener(doneListener);


    final EditText inputDepartment = new EditText(MainActivity.this);
    inputDepartment.setInputType(InputType.TYPE_CLASS_TEXT);
    inputDepartment.setOnEditorActionListener(doneListener);

    final TextView name = new TextView(MainActivity.this); name.setText("Имя устройства"); //name.setLayoutParams(lp);
    final TextView inv = new TextView(MainActivity.this); inv.setText("Инвентарный номер"); //inv.setLayoutParams(lp);
    final TextView dep = new TextView(MainActivity.this); dep.setText("Департамент");// dep.setLayoutParams(lp);
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
        name.setTextColor(Color.WHITE);
        inv.setTextColor(Color.WHITE);
        dep.setTextColor(Color.WHITE);
    }

    layout.addView(name);
    layout.addView(inputName);
    layout.addView(inv);
    layout.addView(inputInv);
    layout.addView(dep);
    layout.addView(inputDepartment);

    alertDialog.setView(layout);
    alertDialog.setPositiveButton("Продолжить",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int which) {
                    SharedPreferences.Editor editor = shPref.edit();
                    String name = inputName.getText().toString() + "-" + inputInv.getText().toString() + "-" + inputDepartment.getText().toString();
                    editor.putString("Name", name);
                    editor.commit();
                    createShortCut(name);
                }
            });

    OnKeyListener okl = new OnKeyListener() {

         @Override
            public boolean onKey(DialogInterface arg0, int keyCode,
                    KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    finish();
                    System.exit(0);
                }
                return true;
            }
        };

    alertDialog.setOnKeyListener(okl);
    alertDialog.show();

doneListener is just closing soft keyboard. Doesn't work without it either.

Seems like a bug with some soft keybords in dialog. Solve this problem by refusing dialog and using Activity instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM