Java ShowSoftInput仅显示一次软按键
在我的应用程序中,我想从中开始在EditText视图中编辑一些文本Java ShowSoftInput仅显示一次软按键,java,android,Java,Android,在我的应用程序中,我想从中开始在EditText视图中编辑一些文本 setFocusableInTouchMode(false). 我想通过按下一个按钮在“编辑模式”和“只读模式”之间切换来实现这一点 我正在使用showsoftwoint和hideSoftInputFromWindow。一切正常,但我无法解决的问题是,软键盘只在第一次自动出现。重复编辑(即反复按下按钮打开和关闭)时,软键盘仅手动重新出现。 这是我的密码 <?xml version="1.0" encoding="utf-
setFocusableInTouchMode(false).
我想通过按下一个按钮在“编辑模式”和“只读模式”之间切换来实现这一点
我正在使用showsoftwoint
和hideSoftInputFromWindow
。一切正常,但我无法解决的问题是,软键盘只在第一次自动出现。重复编辑(即反复按下按钮打开和关闭)时,软键盘仅手动重新出现。
这是我的密码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_act"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/OCRtext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
public class MainActivity extends AppCompatActivity {
boolean TextFound = true;
boolean ON_EDIT = false;
EditText OCRtext;
InputMethodManager imm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OCRtext = findViewById(R.id.OCRtext);
OCRtext.setFocusableInTouchMode(false);
OCRtext.setText(R.string.Testo);
imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_edit: {
if (TextFound) {
if(!ON_EDIT) {
ON_EDIT = true;
//findViewById(R.id.main_act).requestFocus();
Toast.makeText(getApplicationContext(),
"Modifiche abilitate",
Toast.LENGTH_SHORT).show();
OCRtext.setFocusable(true);
OCRtext.setFocusableInTouchMode(true);
OCRtext.setBackgroundColor(getResources().getColor(R.color.black));
OCRtext.setTextColor(getResources().getColor(R.color.white));
OCRtext.requestFocus(); //con questo la keyb compare sempre solo col tap
imm.showSoftInput(OCRtext.getRootView(),2);
}
else {
ON_EDIT = false;
imm.hideSoftInputFromWindow(OCRtext.getWindowToken(),0);
Toast.makeText(getApplicationContext(),
"Modifiche disabilitate",
Toast.LENGTH_SHORT).show();
OCRtext.setFocusable(false);
OCRtext.setFocusableInTouchMode(false);
OCRtext.setBackgroundColor(getResources().getColor(R.color.white));
OCRtext.setTextColor(getResources().getColor(R.color.black));
}
}
}
case R.id.action_save: {
return true;
}
default:
return true;
}
}
}
公共类MainActivity扩展了AppCompatActivity{
布尔TextFound=true;
编辑上的布尔值=false;
编辑文本;
输入方法管理器imm;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OCRtext=findviewbyd(R.id.OCRtext);
OCRtext.setFocusableInTouchMode(false);
setText(R.string.Testo);
imm=(InputMethodManager)getSystemService(Context.INPUT\u方法\u服务);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
开关(id){
案例R.id.action\u编辑:{
如果(TextFound){
如果(!ON_编辑){
ON_EDIT=true;
//findViewById(R.id.main_act).requestFocus();
Toast.makeText(getApplicationContext(),
“修改能力”,
吐司。长度(短)。show();
OCRtext.setFocusable(true);
OCRtext.setFocusableInTouchMode(true);
setBackgroundColor(getResources().getColor(R.color.black));
OCRtext.setTextColor(getResources().getColor(R.color.white));
OCRtext.requestFocus();//请求键盘比较sempre solo col tap
imm.showSoftInput(OCRtext.getRootView(),2);
}
否则{
ON_EDIT=false;
imm.hideSoftInputFromWindow(OCRtext.getWindowToken(),0);
Toast.makeText(getApplicationContext(),
“修改使失能”,
吐司。长度(短)。show();
OCRtext.setFocusable(false);
OCRtext.setFocusableInTouchMode(false);
setBackgroundColor(getResources().getColor(R.color.white));
OCRtext.setTextColor(getResources().getColor(R.color.black));
}
}
}
案例R.id.action\u保存:{
返回true;
}
违约:
返回true;
}
}
}
格式化问题并修改语法格式化问题并修改语法