将焦点设置在EditText上
我有一个EditText字段,并为它设置一个OnFocusChangeListener。 当它失去焦点时,会调用一个方法,该方法将EditText的值与数据库中的一个进行比较。 如果方法的返回值为true,则会显示一个祝酒,焦点应该再次返回到EditText。 焦点应该总是回到EditText并且键盘应该显示,直到方法的返回值为false。
编辑:我想,我还没有完全清楚我的真正问题:屏幕上没有其他项目应该能够编辑,直到EditText的值被编辑为一个值,这使得方法“checkLiganame(liganame) “返回false。 只有EditText-Field应该是可编辑的。
这里是我的代码(这对我不起作用):
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,但我无法再编辑文本。
编辑2:更改我的代码。 场景:
我点击第一个EditText并在其中放入一个字符串,它已经在数据库中。 吐司正在展示。 现在我不能编辑我的字符串了。 我点击键盘上的“下一步”,焦点停留在第一个EditText上。 我尝试编辑我的字符串,但没有任何反应。 相反,我的新字符串显示在第二个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);
}
编辑:添加checkLiganame方法后添加额外的信息到答案。
在checkLiganame方法中,检查游标是否为空。 游标将始终返回一个对象,因此检查null不会执行任何操作。 然而,问题是在行db.close();
当您关闭数据库连接时, Cursor
变得无效并且最可能是空的。
因此,在获取值之后关闭数据库。
您应该检查返回的行数是否大于0,而不是检查游标是否为空:if(cursor.getCount()> 0),如果是,则将布尔值设置为true。
编辑2:所以这里有一些如何使它工作的代码。 编辑3:对不起,我添加的错误代码...; 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
设置为同一个侦听器,就像我在示例中所做的那样。
Darwind代码没有显示键盘。
这适用于我:
_searchText.requestFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(_searchText, InputMethodManager.SHOW_IMPLICIT);
如果键盘未显示,请尝试强制:
imm.showSoftInput(_searchText, InputMethodManager.SHOW_FORCED);
链接地址: http://www.djcxy.com/p/24223.html