Android 是否将ImageButton设置为不可见,并在按下手机的home/back(主页/后退)按钮或方向改变时将其保留不起作用?
在我的应用程序中,EditText中有一个ImageButton。在我的应用程序中单击ImageButton时,EditText中有一个ImageButton。单击ImageButton时,我使用VIEW.GONE使其不可见,并在EditText中输入一些文本。当按下手机的home(主页)按钮、back(后退)按钮以及方向改变时,我希望将ImageButton保持为不可见。有人能帮我吗,这怎么办 以下是代码的一部分: 在Oncreate方法中:Android 是否将ImageButton设置为不可见,并在按下手机的home/back(主页/后退)按钮或方向改变时将其保留不起作用?,android,visibility,imagebutton,onclicklistener,Android,Visibility,Imagebutton,Onclicklistener,在我的应用程序中,EditText中有一个ImageButton。在我的应用程序中单击ImageButton时,EditText中有一个ImageButton。单击ImageButton时,我使用VIEW.GONE使其不可见,并在EditText中输入一些文本。当按下手机的home(主页)按钮、back(后退)按钮以及方向改变时,我希望将ImageButton保持为不可见。有人能帮我吗,这怎么办 以下是代码的一部分: 在Oncreate方法中: protected void onCreate(B
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_flip_card);
edt_aroundMsg = (EditText) findViewById(R.id.edt_aroundMsg);
if(savedInstanceState!=null){
onRestoreInstanceState(savedInstanceState);
}
}
ImageButton onClickListener:
btn_msg_tap = (ImageButton) findViewById(R.id.btn_msg_tap);
btn_msg_tap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
btn_msg_tap.setVisibility(View.GONE);
edt_aroundMsg.setPadding(20, 10, 0, 0);
edt_aroundMsg.setEnabled(true);
// edt_aroundMsg.setFocusableInTouchMode(true);
edt_aroundMsg.requestFocus();
InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
在onSaveInstanceState和onRestoreInstanceState方法中:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("message", edt_aroundMsg.getText().toString());
outState.putInt("font_size", font_size);
outState.putBoolean("edt_msg_enabled", edt_aroundMsg.isEnabled());
//outState.putByte("font_style", tf);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
edt_enabled = savedInstanceState.getBoolean("edt_msg_enabled");
/*if (edt_enabled == true){
btn_msg_tap.setVisibility(View.INVISIBLE);
}
else{
btn_msg_tap.setVisibility(View.VISIBLE);
}*/
str_edtMsg = savedInstanceState.getString("message");
font_size = savedInstanceState.getInt("font_size");
edt_aroundMsg.setText(str_edtMsg);
edt_aroundMsg.setTextSize(font_size);
}
我可能遗漏了一些东西,因为我没有经验,所以解决这个问题有困难。请尽快帮助我。谢谢。当按下主页按钮时,您可以在此处检测: 在每个活动中输入以下代码
@Override
public void onPause(){
super.onPause();
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
Toast.makeText(MyActivity.this, "You Left your App", Toast.LENGTH_SHORT).show();
// retain the ImageButton as invisible .
}
}
}
检测方向变化:
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
//You also have to edit the appropriate element in your manifest file to include the //android:configChanges Just see the code below:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">
public void onconfiguration已更改(配置newConfig){
super.onConfigurationChanged(newConfig);
//检查屏幕的方向
if(newConfig.orientation==Configuration.orientation\u横向){
Toast.makeText(此为“横向”,Toast.LENGTH_SHORT).show();
}else if(newConfig.orientation==Configuration.orientation\u纵向){
Toast.makeText(这是“肖像”,Toast.LENGTH_SHORT).show();
}
}
//您还必须编辑清单文件中的相应元素,以包含//android:configChanges,请参见下面的代码:
ImageButton的可见性没有问题,因为您知道如何更改其可见性。但问题在于如何检测按下的主页/后退按钮。关于如何检测按下的主页/后退按钮,有很多问题。请在询问之前仔细搜索。@Geet谢谢你的建议。我搜索了很多,但都没能解决问题。这就是为什么我发帖子希望得到一个能解决我问题的答案。我已经更新了上面的代码,大多数人都将其作为此类问题的解决方案,但它不起作用,或者我可能遗漏了一些内容。是否必须添加这一行?xmppclient.disconnect(getBaseContext());我读过这篇文章,说xmppclient是一个软件,它可以让你连接到XMPP,通过互联网与其他人进行即时消息传递。这与我的申请无关。这句话不是我写的,也没用。ImageButton再次可见。但是您共享的所有这些方法都不起作用。每当按下返回/主页按钮以及方向改变时,我仍然可以看到ImageButton。如果我把我的代码贴出来让你们看看发生了什么,会不会有帮助?当按下“主页/返回”按钮或方向改变时,你们会得到祝酒词吗?如果你得到了祝酒词,那么我的回答没有错,所以你不能说这种方法不起作用。可能是它的可见性问题,而你当时没有得到。因此,请验证其可见性。
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
}
//You also have to edit the appropriate element in your manifest file to include the //android:configChanges Just see the code below:
<activity android:name=".MyActivity"
android:configChanges="orientation|keyboardHidden"
android:label="@string/app_name">