我有一个 EditText-Field 并为它设置了一个 OnFocusChangeListener。当它失去焦点时,会调用一个方法,该方法将 EditText 的值与数据库中的一个进行检查。如果该方法的返回值为 true,则会显示一个 toast,并且焦点应该再次回到 EditText 上。焦点应该总是回到 EditText 并且键盘应该显示,直到方法的返回值为 false。
编辑:我想,我的真正问题还没有完全清楚:屏幕上没有其他项目应该能够编辑,直到 EditText 的值被编辑为一个值,这使得方法“checkLiganame(liganame) "返回假。只有 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,但我无法再编辑文本。
EDIT2:更改了我的代码。设想:
我单击第一个 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。
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
设置为同一个侦听器,就像我在示例中所做的那样。
Darwind 代码没有显示键盘。
这对我有用:
_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);
}
单击按钮时,这会更改 EditText 的焦点:
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);
setFocusable
需要 API 26。在我的情况下也不需要 setFocusableInTouchMode
。
如果我们动态创建 EditText,那么我们必须设置 requestFocus(),如下所示。
EditText editText = new EditText(this);
editText.setWidth(600);
editText.requestFocus();
如果我们已经在 xml 视图中声明了该组件,那么我们必须找到它,我们可以如下所示关注焦点。
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);
}
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();
}
});
如果您尝试在布局膨胀之前调用 requestFocus()
,它将返回 false。此代码在布局膨胀后运行。您不需要如 above 所述的 200 毫秒延迟。
editText.post(Runnable {
if(editText.requestFocus()) {
val imm = editText.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
imm?.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0)
}
})
private void requestFocus(View view) {
if (view.requestFocus()) {
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
}
//Function Call
requestFocus(yourEditetxt);
你可以用一行来做到这一点:
yourEditText.RequestFocusFromTouch();
对于 Xamarin.Android,我创建了这个扩展。
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);
}
}
}
如果在 onCreate()
中使用 requestFocus()
会导致键盘在点击时不显示的问题,请使用 BindingAdapter
并在其中请求焦点。
这是如何做到的:
绑定适配器
@BindingAdapter("requestFocus")
fun bindRequestFocus(editText: EditText, event: Event<Boolean>?) {
event?.getContentIfNotHandled()?.let {
if (it) editText.requestFocus()
}
}
我的回答here
正如我在官方文档上阅读的那样,我认为这是最好的答案,只需将 View 传递给 EditText 等参数,但 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)
}
}
我不知道您是否已经找到了解决方案,但是对于再次请求焦点后的编辑问题:
您是否尝试在您的 edittext1 上调用方法 selectAll()
或 setSelection(0)
(如果是 emtpy)?
请让我知道这是否有帮助,因此我将编辑我的答案以提供完整的解决方案。
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>
不定期副业成功案例分享