在我的xml文件中,我有两个带有RelativeLayout的按钮。在我的类中,我扩展了Dialog并实现了OnClickListener,并添加了OnClick(View v)方法。但是当单击按钮时,一些onClick代码从未被执行。有人能帮我找到代码问题吗:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="vertical"
android:padding="10px">
......
<Button android:id="@+id/saveBtn_settingDlg" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_below="@+id/editText1"
android:layout_marginLeft="10px" android:text="Save" />
<Button android:id="@+id/closeBtn_settingDlg" android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Close" android:layout_alignBaseline="@+id/saveBtn_setting"
android:layout_toRightOf="@+id/saveBtn_setting" android:onClick="CloseDialog" />
类
public class SettingDialog extends Dialog implements OnClickListener {
private Button btn_save, btn_close;
// In Constructor
btn_save = (Button) findViewById(R.id.saveBtn_settingDlg);
btn_close = (Button) findViewById(R.id.closeBtn_settingDlg);
btn_save.setOnClickListener(this);
btn_close.setOnClickListener(this);
@Override
public void onClick(View v) {
if (v == btn_save)
SaveSettings();
else if (v == btn_close)
CloseDialog();
return;
}
private void CloseDialog() {
disposeAll();
this.dismiss();
}
public void CloseBtnClicked(View v) {
CloseDialog();
}
在XML中,我尝试使用CloseBtnClicked作为关闭按钮,但没有任何区别,并且我收到了一个UnexpectedError消息并且应用程序关闭。不知何故,该事件无论如何都不会被激活。而且,在将onClick添加到closebtn后,该按钮现在显示在屏幕的左上方,失去了其实际位置。
从Activity类中调用SettingDialog:
private void OpenSettingDialog() {
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(R.drawable.ic_dialog_small);
View inflatedView = LayoutInflater.from(this).inflate(R.layout.settings_dialog, null);
ad.setView(inflatedView);
AlertDialog adlg = ad.create();
adlg.show();
}
有谁能帮助我了解这个问题的原因以及如何解决它。我是 Android 的新手。
谢谢