Android EditText 请求焦点
Android EditText requestFocus
我想动态创建待办事项(单选按钮)。所以我创建了水平方向的线性布局,并将 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();
}
});```
在调用 requestFocus 后添加这些行:
edNew.requestFocus();
InputMethodManager im= (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(edNew, InputMethodManager.SHOW_IMPLICIT);
我想动态创建待办事项(单选按钮)。所以我创建了水平方向的线性布局,并将 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();
}
});```
在调用 requestFocus 后添加这些行:
edNew.requestFocus();
InputMethodManager im= (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
im.showSoftInput(edNew, InputMethodManager.SHOW_IMPLICIT);