我想通过图像完成一项活动,这就是我所做的:
布局[activity_login.xml]中有一个ImageView。
/**
* the activity that I want to destroy
*/
public class LoginActivity extends Activity {
public static LoginActivity activityInstance;
private ImageView imgBtnBack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// for closing this activity by an another class
activityInstance = this;
// add event listener
imgBtnBack = (ImageView) findViewById(R.id.img_btn_back);
imgBtnBack.setOnClickListener(new LoginActivityListener());
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
/**
* the event handler of LoginActivity
*/
public class LoginActivityListener implements View.OnClickListener{
private Context context;
@Override
public void onClick(View view) {
context = view.getContext();
int id = view.getId();
switch (id) {
case R.id.img_btn_back: // close the activity by an image
LoginActivity.activityInstance.finish();
default:
break;
}
}
}
我不知道我的做法是否好。 任何人都可以找到并告诉我一个更好的方法来使这项工作。
答案 0 :(得分:0)
在onCreate方法中添加以下代码
imgBtnBack = (ImageView) findViewById(R.id.img_btn_back);
imgBtnBack .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});