具有EditText的TableRow在键盘消失之前不会显示文本

时间:2019-02-02 12:45:15

标签: android android-studio android-layout android-fragments android-framelayout

我正在通过运行时动态创建视图。码。它是在xml布局文件中创建的TableLayout标签。然后在代码中,我添加带有SpinnerEditText的表行,两个这样的行。问题是EditText被添加了一定的显示文本,直到键盘消失为止。尝试了许多替代方法,例如将背景设置为与文本颜色不同的颜色,但仍然可以正常工作。

如果有人可以提出解决方案,我将不胜感激。这是创建动态布局的源代码。

    private void createRowRange() {

    cnt++;
    listCnt.add(cnt + "");

    final TableRow row = new TableRow(getActivity());

    String one = String.valueOf(cnt) + "1";
    String two = String.valueOf(cnt) + "2";
    String three = String.valueOf(cnt) + "3";
    String four = String.valueOf(cnt) + "4";

    ImageView imgRem = new ImageView(getActivity());
    imgRem.setId(cnt);
    row.setId(generateViewId());

    row.addView(imgRem);

    final TableRow row1 = new TableRow(getActivity());

    final Spinner spinnerEquipment = new Spinner(getActivity());
    spinnerEquipment.setId(Integer.parseInt(one));
    spinnerEquipment.setMinimumWidth(400);

    ArrayList<String> array = new ArrayList<>();
    array.add(getString(R.string.select_price_per));
    array.add("Distance (Km)");
    array.add("Hour");

    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, array);

    spinnerEquipment.setAdapter(adapter);

    final EditText et1 = new EditText(getActivity());
    et1.setHint("Price");
    et1.setWidth(300);
    et1.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorBlack));
    et1.setInputType(InputType.TYPE_CLASS_NUMBER);
    et1.setPadding(15, 15, 15, 15);
    et1.setId(Integer.parseInt(two));
    et1.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.input_outline));

    row1.addView(spinnerEquipment);
    row1.addView(et1);


    final TableRow row2 = new TableRow(getActivity());

    final Spinner spinnerEquipment2 = new Spinner(getActivity());
    spinnerEquipment2.setId(Integer.parseInt(three));
    spinnerEquipment.setMinimumWidth(400);

    final ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, equipmentArray);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerEquipment2.setAdapter(adapter2);

    final EditText et2 = new EditText(getActivity());
    et2.setHint("Fixed Price");
    et2.setWidth(300);

    et2.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorBlack));
    et2.setInputType(InputType.TYPE_CLASS_NUMBER);
    et2.setPadding(15, 15, 15, 15);
    et2.setId(Integer.parseInt(four));

    et2.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.input_outline));

    row2.addView(spinnerEquipment2);
    row2.addView(et2);

    imgRem.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.mipmap.ic_minus));

    imgRem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            tableRange.removeView(row);
            tableRange.removeView(row1);
            tableRange.removeView(row2);
            Log.d(TAG, "Count :: " + v.getId() + "");
            listCnt.remove(v.getId() + "");

            for (String i : listCnt)
                Log.d(TAG, i + "");
        }
    });

    tableRange.addView(row);
    tableRange.addView(row2);
    tableRange.addView(row1);
}

View created dynamically using code

1 个答案:

答案 0 :(得分:0)

您应该将请求焦点设置为EditText

et2.requestFocus()

并添加 AndroidManifest.xml

android:windowSoftInputMode="adjustPan"