Android EditText requestFocus

时间:2020-05-30 12:08:52

标签: android android-edittext

我想创建动态执行的项目(单选按钮)。因此,我创建了具有水平方向的线性布局,并将radioButton和editText推入其中,并且我需要将重点放在此EditText上。我尝试使用方法requestFocus(),但对我而言不起作用。我在做错什么吗?

btnToDo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LinearLayout llNew= new LinearLayout(NewNote.this);
                llNew.setOrientation(LinearLayout.HORIZONTAL);
                RadioButton rbNew= new RadioButton(NewNote.this);
                rbNew.setPadding(10,0,10,0);

                //set radio button color:
                if(Build.VERSION.SDK_INT>=21)
                {
                    ColorStateList colorStateList = new ColorStateList(
                            new int[][]{
                                    new int[]{-android.R.attr.state_enabled}, //disabled
                                    new int[]{android.R.attr.state_enabled} //enabled
                            },
                            new int[] {
                                    Color.BLACK //disabled
                                    ,Color.BLACK //enabled
                            }
                    );
                    rbNew.setButtonTintList(colorStateList);//set the color tint list
                    rbNew.invalidate(); //could not be necessary
                }

                final EditText edNew = new EditText(NewNote.this);
                edNew.setTypeface(typefaceDunkin);
                edNew.setTextSize(25);
                edNew.setText("Some text");
                edNew.setBackgroundColor(Color.WHITE);

                edNew.requestFocus(); //HERE

                //cross text after click on radio button
                rbNew.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        edNew.setPaintFlags(edNew.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
                    }
                });

                llNew.addView(rbNew);
                llNew.addView(edNew);
                content.addView(llNew);
                scrollViewDown();
            }
        });```

1 个答案:

答案 0 :(得分:0)

在调用requestFocus之后添加这些行:


Set a       "operations"     / 1, 2, 3 /
    b       "operations"     / 1, 2, 3 /
    in(a,b) "Mapping all to all" / #a.#b /
    in2(a,b) "Selection of pairs of operations" ;
    in2(a,b)$(ord(b)=ord(a)+1)=yes;

display in, in2;