텍스트 편집에 포커스 설정
EditText-Field가 있고 OnFocusChangeListener를 설정합니다.포커스가 손실되면 데이터베이스에 있는 EditText 값을 확인하는 메서드가 호출됩니다.메소드의 반환 값이 참이면 건배가 표시되고 포커스가 다시 EditText(텍스트 편집)로 돌아갑니다.메서드의 반환 값이 거짓이 될 때까지 항상 초점이 EditText로 돌아가고 키보드가 표시되어야 합니다.
편집: 제 생각에는 아직 제 진짜 문제를 완벽하게 명확하게 하지 않았습니다. 편집 텍스트의 값이 값으로 편집될 때까지 화면의 다른 항목은 편집할 수 없으며, 이로 인해 "checkLiganame(liganame)" 메서드가 거짓으로 반환됩니다.텍스트 필드 편집만 편집 가능해야 합니다.
여기 제 코드가 있습니다. (저에게는 통하지 않는):
final EditText Liganame = (EditText) findViewById(R.id.liganame);
Liganame.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
String liganame = Liganame.getText().toString();
if (checkLiganame(liganame)) {
Toast toast = Toast.makeText(CreateTableActivity.this,
"Dieser Liganame ist bereits vergeben",
Toast.LENGTH_SHORT);
toast.show();
Liganame.requestFocus();
}
}
방법:
public boolean checkLiganame(String liganame) {
boolean found = false;
DatabaseHelper databaseHelper = new DatabaseHelper(this);
SQLiteDatabase db = databaseHelper.getReadableDatabase();
Cursor cursor = db.query("liga", new String[] { "liganame" },
"liganame = '" + liganame + "'", null, null, null, null);
Log.i("Liganame: ", String.valueOf(cursor));
db.close();
if (cursor != null) {
found = true;
}
return found;
}
이 코드로 인해 EditText가 포커스를 잃은 후 포커스가 EditText로 다시 이동하지만 더 이상 텍스트를 편집할 수 없습니다.
EDIT2: 코드를 바꿨습니다.시나리오:
첫 번째 EditText를 클릭하고 데이터베이스에 이미 있는 문자열을 입력합니다.토스트가 나오고 있습니다.이제 더 이상 String을 편집할 수 없습니다.키보드에서 "다음"을 클릭하면 첫 번째 EditText에 포커스가 유지됩니다.String을 편집하려고 하지만 아무 일도 일어나지 않습니다.대신 새 문자열이 두 번째 EditText에 표시됩니다.단말기의 뒷 화살표를 클릭하고 첫 번째와 두 번째 EditText --> 키보드가 표시되지 않습니다.
내 새 코드는 다음과 같습니다.
public class CreateTableActivity extends Activity implements
OnFocusChangeListener {
private EditText Liganame, Mannschaftsanzahl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_league);
Liganame = (EditText) findViewById(R.id.liganame);
Liganame.setOnFocusChangeListener(this);
Mannschaftsanzahl = (EditText) findViewById(R.id.mannschaftsanzahl);
Mannschaftsanzahl.setOnFocusChangeListener(this);
final Button save_button = (Button) findViewById(R.id.create_tabelle_speichern_button);
OnClickListener mCorkyListener = new OnClickListener() {
public void onClick(View v) {
ButtonClick();
}
};
save_button.setOnClickListener(mCorkyListener);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
String liganame = Liganame.getText().toString();
if (checkLiganame(liganame)) {
if (Liganame.requestFocus()) {
getWindow()
.setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
Mannschaftsanzahl.clearFocus();
Toast.makeText(CreateTableActivity.this,
"Dieser Liganame ist bereits vergeben",
Toast.LENGTH_SHORT).show();
}
}
}
이 선을 당신의 것에 대세요.onCreate()
editText.requestFocus();
초점을 맞추는 것은 키보드를 보여주기에 충분하지 않습니다.
초점을 맞추고 키보드를 보여주기 위해서는 다음과 같이 적어야 합니다.
if(myEditText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
EDIT : checkLiganame 메서드 추가 후 답변에 추가 정보 추가
checkLiganame 메서드에서 커서가 null인지 확인합니다.커서는 항상 개체를 반환하므로 null을 확인해도 아무 효과가 없습니다.그러나 문제는 그 문제에 있습니다.db.close();
데이터베이스 연결을 닫을 때Cursor
유효하지 않게 되고 아마도 null이 될 것입니다.
값을 가져온 후 데이터베이스를 닫으십시오.
커서가 null인지 확인하는 대신 반환되는 행 수가 0보다 큰지 확인한 다음(cursor.getCount() > 0) 부울을 true로 설정해야 합니다.
EDIT2: 여기 작동 방법에 대한 코드가 있습니다.EDIT3: 코드를 잘못 추가해서 죄송합니다.. ;S
우선, 당신은 다른 사람이 다른 사람과EditText
초점을 잡습니다.이 작업은 다음과 같이 수행할 수 있습니다.myEditText.clearFocus()
. 그런 다음 OnFocusChangeListener를 사용할 때는 처음부터 신경쓰지 않아도 됩니다.EditText
를 사용하거나 사용하지 않을 수 있으므로 onFocusChangeListener는 다음과 같이 보일 수 있습니다.
public class MainActivity extends Activity implements OnFocusChangeListener {
private EditText editText1, editText2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = (EditText) findViewById(R.id.editText1);
editText1.setOnFocusChangeListener(this);
editText2 = (EditText) findViewById(R.id.editText2);
editText2.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
String liganame = editText1.getText().toString();
if(liganame.length() == 0) {
if(editText1.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
editText2.clearFocus();
Toast.makeText(MainActivity.this, "Dieser Liganame ist bereits vergeben", Toast.LENGTH_SHORT).show();
}
}
}
}
첫번째 체크 바꾸기if(liganame.length() == 0)
당신의 수표가 있으면, 작동할 겁니다.모든 EditText 보기는 다음을 설정해야 합니다.onFocusChangeListener
예에서 했던 것과 같은 청취자에게 말입니다.
다윈드 코드가 키보드를 보여주지 않았습니다.
이것은 저에게 효과가 있습니다.
_searchText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);
키보드가 표시되지 않을 경우 강제로 다음을 수행합니다.
imm.showSoftInput(_searchText, InputMethodManager.SHOW_FORCED);
이것은 내가 만든 것입니다.
public void showKeyboard(final EditText ettext){
ettext.requestFocus();
ettext.postDelayed(new Runnable(){
@Override public void run(){
InputMethodManager keyboard=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(ettext,0);
}
}
,200);
}
숨기기:
private void hideSoftKeyboard(EditText ettext){
InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(ettext.getWindowToken(), 0);
}
이렇게 하면 단추를 클릭하면 텍스트 편집의 포커스가 변경됩니다.
public class MainActivity extends Activity {
private EditText e1,e2;
private Button b1,b2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1=(EditText) findViewById(R.id.editText1);
e2=(EditText) findViewById(R.id.editText2);
e1.requestFocus();
b1=(Button) findViewById(R.id.one);
b2=(Button) findViewById(R.id.two);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
e1.requestFocus();
}
});
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
e2.requestFocus();
}
});
}
}
이것이 저에게 효과가 있었고 초점을 맞추고 키보드도 보여주었습니다.
EditText userNameText = (EditText) findViewById(R.id.textViewUserNameText);
userNameText.setFocusable(true);
userNameText.setFocusableInTouchMode(true);
userNameText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(userNameText, InputMethodManager.SHOW_IMPLICIT);
동적으로 EditText를 생성하면 아래와 같이 요청 Focus()를 설정해야 합니다.
EditText editText = new EditText(this);
editText.setWidth(600);
editText.requestFocus();
만약 우리가 이미 xml 보기에 component를 선언했다면 우리는 그것을 찾아야 하고 아래와 같이 포커스를 맞출 수 있습니다.
EditText e1=(EditText) findViewById(R.id.editText1);
e1.requestFocus();
해당 EditText 구성요소에만 포커스를 설정합니다.
mEditText.setFocusableInTouchMode(true);
mEditText.requestFocus();
if(mEditText.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
전화하시려고 하면requestFocus()
레이아웃을 부풀리기 전에 false를 반환합니다.이 코드는 레이아웃이 팽창된 후에 실행됩니다.위와 같이 200ms 지연은 필요 없습니다.
editText.post(Runnable {
if(editText.requestFocus()) {
val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm?.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
}
})
Button btnClear = (Button) findViewById(R.id.btnClear);
EditText editText1=(EditText) findViewById(R.id.editText2);
EditText editText2=(EditText) findViewById(R.id.editText3);
btnClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText1.setText("");
editText2.setText("");
editText1.requestFocus();
}
});
나의 대답은 여기에
공식 문서를 읽어보니 이게 최선의 답이라고 생각합니다. EditText와 같은 View to 매개 변수를 전달하면 되지만 showSoftKeyboard가 풍경에서 작동하지 않는 것 같습니다.
private fun showSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT)
}
}
private fun closeSoftKeyboard(view: View) {
if (view.requestFocus()) {
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
imm.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
//Function Call
requestFocus(yourEditetxt);
이 작업은 한 줄로 수행할 수 있습니다.
yourEditText.RequestFocusFromTouch();
Xamarin을 위해서.안드로이드 나는 이 확장자를 만들었습니다.
public static class ViewExtensions
{
public static void FocusEditText(this EditText editText, Activity activity)
{
if (editText.RequestFocus())
{
InputMethodManager imm = (InputMethodManager)activity.GetSystemService(Context.InputMethodService);
imm.ShowSoftInput(editText, ShowFlags.Implicit);
}
}
}
사용하는 경우requestFocus()
인에onCreate()
탭에 키보드가 표시되지 않는 문제를 소개합니다.BindingAdapter
SingleLiveEvent를 사용하고 그 안에 포커스를 요청합니다.
다음과 같은 방법이 있습니다.
바인딩 어댑터
@BindingAdapter("requestFocus")
fun bindRequestFocus(editText: EditText, event: Event<Boolean>?) {
event?.getContentIfNotHandled()?.let {
if (it) editText.requestFocus()
}
}
이미 해결책을 찾으셨는지 모르겠지만, 포커스를 다시 요청한 후 편집 문제에 대해:
그 방법으로 전화를 해보셨나요?selectAll()
아니면setSelection(0)
(emtpy인 경우) 편집 텍스트1에?
이것이 도움이 되는지 알려주세요, 그래서 제가 완벽한 해결책에 대한 답변을 수정하겠습니다.
new OnEditorActionListener(){
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
editText.requestFocus();
//used ******* return true ******
return **true**;
}
}
매니페스트에 이 코드를 사용해 보십시오.
<activity android:name=".EditTextActivity" android:windowSoftInputMode="stateAlwaysVisible">
</activity>
언급URL : https://stackoverflow.com/questions/14327412/set-focus-on-edittext
'programing' 카테고리의 다른 글
종료 시 파일 설명자를 닫는 것이 좋은 방법입니까? (0) | 2023.10.31 |
---|---|
Java Virtual Machine(JVM) 및 성능 비교 (0) | 2023.10.31 |
npm 설치를 실행할 때 첫 번째 인증서를 확인할 수 없음 (0) | 2023.10.31 |
UI 부트스트랩 팝업: 너비 변경 (0) | 2023.10.31 |
조건부로 angular.js에 요소 특성을 추가합니다. (0) | 2023.10.31 |