java 关闭android中的活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17539531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dismiss activity in android
提问by indira
I am using the following code to show “Hello” message with ok button when the user clicks a button. In some applications this is working fine. ie while clicking the ok button, the activity is dismissed. But in one application, this is not getting dismissed after clicking the ok button. What to do? Please help.
我使用以下代码在用户单击按钮时显示带有确定按钮的“Hello”消息。在某些应用程序中,这工作正常。即在单击确定按钮时,活动被解除。但在一个应用程序中,单击“确定”按钮后不会取消此操作。该怎么办?请帮忙。
public class MyClass extends Activity {
private TextView labelTxt;
private Button okBtn;
@Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.message);
labelTxt = (TextView) findViewById(R.id.txt);
labelTxt.setText("Hello");
okBtn = (Button) findViewById(R.id.okBtn);
okBtn.setOnClickListener(okBtnClickListener);
}
private final OnClickListener okBtnClickListener = new OnClickListener() {
public void onClick(View v) {
finish();
}
};
}
回答by Ponmalar
I have tested your code and modified little. Please check below
我已经测试了您的代码并几乎没有修改。请检查以下
package test.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btnOK);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
回答by Ram kiran
Try like this....
试试这样....
okBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();// Closing Activity
}
});
回答by heLL0
In your layout make sure your Button is named:
在您的布局中,确保您的 Button 被命名为:
android:id="@+id/okBtn"
.....
Did you import:
您是否导入了:
import android.view.View.OnClickListener;
Please look at this tutorial, it should help:
请查看本教程,它应该会有所帮助:
http://martin.cubeactive.com/android-onclicklitener-tutorial/
http://martin.cubeactive.com/android-onclicklitener-tutorial/