安卓:按钮点击事件 [英] Android: Button click event

查看:218
本文介绍了安卓:按钮点击事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与RelativeLayout的XML文件的2个按钮。在我的课堂我已经扩展对话框和放大器; implemetned OnClickListener还增加的OnClick(查看v)的方法。但不知何故,onClick的code永远不会执行按钮被点击时。谁能帮我找到我的code中的问题:

I have 2 buttons in my xml file with RelativeLayout. In my class I have extended Dialog & implemetned OnClickListener and also added OnClick(View v) method. But somehow the onClick code is never executed when the button is clicked. Can anyone help me find the problem with my code :

<?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密切BTN我试图CloseBtnClicked,但也没有什么区别,我得到一个UnexpectedError消息和应用程序关闭。不知不觉中,这些事件不仅没有以任何方式激活。并且还对加入的onClick到closebtn按钮现在显示在屏幕的左上角,失去了它的实际位置。

In xml for close btn I tried CloseBtnClicked also but no difference and I get an UnexpectedError message and application shuts down. Somehow the event is only not activated in any ways. And also on adding onClick to closebtn the button is now shown on the top-left of the screen and lost the actual location of it.

这是Activity类调用SettingDialog:

Calling SettingDialog from Activity class :

    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。

Can anyone help me know the reason for this problem and how do I solve it. I am a newbie to Android.

感谢

推荐答案

解决我的问题:

而不是使用AlertBuilder和AlertDialog的,我只是叫对话框如下:

Instead of using AlertBuilder and AlertDialog, I just called the dialog as :

    SettingDialog sd = new SettingDialog(this, mySettings);
sd.show();

和它运行良好。所有的点击事件中SettingDialog只处理。没有变化是在SettingDialog作出。只有调用SettingDialog的方式在活动发生变化。就是这样。

And this worked well. All click events were handled within SettingDialog only. No changes were to be made in SettingDialog. Only the way to call SettingDialog is changed in the Activity. That's it.

顺便说一句,在的onClick()comapring其名称的视图:

BTW, In onClick() comapring a View with its name :

    public void onClick(View v) {
    Log.i("APP: ", "Into OnClick of SettingDialog. View = " + v);
    if (v == btn_save) 
        SaveSettings();
    else if (v == btn_close) 
        CloseDialog();

    return;
}

也完美的作品。我用这种方式只能和它工作得很好。不需要检查,只有编号。

Also works perfectly. I use this way only and it works well. No need to check with the Id only.

希望我的解决方案将帮助其他人谁被卡住像我一样。 感谢大家的努力,并伸出援助之手。

Hope my solution will help others who are stuck like me. Thanks to all for your efforts and helping hand.

这篇关于安卓:按钮点击事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆