简体   繁体   English

Android:按钮点击事件

[英]Android: Button click event

I have 2 buttons in my xml file with RelativeLayout. 我在带有RelativeLayout的xml文件中有2个按钮。 In my class I have extended Dialog & implemetned OnClickListener and also added OnClick(View v) method. 在我的课堂上,我扩展了Dialog并实现了OnClickListener,还添加了OnClick(View v)方法。 But somehow the onClick code is never executed when the button is clicked. 但是以某种方式单击按钮时永远不会执行onClick代码。 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"  />

Class

 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();
}

In xml for close btn I tried CloseBtnClicked also but no difference and I get an UnexpectedError message and application shuts down. 在关闭btn的xml中,我也尝试了CloseBtnClicked,但没有区别,我收到UnexpectedError消息,应用程序关闭。 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. 并且在将onClick添加到closebtn时,该按钮现在显示在屏幕的左上角,并且丢失了其实际位置。

Calling SettingDialog from Activity class : 从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();

}

Can anyone help me know the reason for this problem and how do I solve it. 谁能帮助我知道这个问题的原因以及如何解决。 I am a newbie to Android. 我是Android的新手。

Thanks 谢谢

You should turn to use the simplest way that I always do as below: 您应该转而使用我经常执行的最简单的方法,如下所示:

@Override
public void onCreate(Bundle savedInstanceState) {
    button1.setOnClickListener(onClickListener);
    button2.setOnClickListener(onClickListener);
    button3.setOnClickListener(onClickListener);
}

private OnClickListener onClickListener = new OnClickListener() {
    @Override
    public void onClick(final View v) {
        switch(v.getId()){
            case R.id.button1:
                 //DO something
            break;
            case R.id.button2:
                 //DO something
            break;
            case R.id.button3:
                 //DO something
            break;
        }
    }
};

i think you should compare the view id's not views 我认为您应该比较视图ID而不是视图

if (v == btn_save)

to

   if (v.getId() == btn_save.getId())
android:onClick="CloseDialog"

of Button in layout for Dialog searches the method in Activity class not in Dialog Dialog布局中Button的数量搜索不在Dialog中的Activity类中的方法

define your method in Activity which is calling Dialog or remove android:onClick="CloseDialog" from tag and set OnClickListener from Java code in Dialog class. 在调用Dialog Activity定义您的方法,或从标记中删除android:onClick="CloseDialog"并从Dialog类的Java代码中设置OnClickListener

Button Name is MyButton.it's working. 按钮名称为MyButton。正在运行。

   MyButton.setOnClickListener(new OnClickListener() 
{
 @Override          
 public void onClick(View v) 
 {              
     mytextView.setText("Messi");           
  }         
});

Solution to my Problem : 解决我的问题的方法:

Instead of using AlertBuilder and AlertDialog, I just called the dialog as : 我没有使用AlertBuilder和AlertDialog,而是将对话框称为:

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

And this worked well. 而且效果很好。 All click events were handled within SettingDialog only. 所有点击事件仅在SettingDialog中处理。 No changes were to be made in SettingDialog. 在SettingDialog中无法进行任何更改。 Only the way to call SettingDialog is changed in the Activity. 活动中仅更改了调用SettingDialog的方式。 That's it. 而已。

BTW, In onClick() comapring a View with its name : 顺便说一句,在onClick()中,将视图与其名称共同映射:

    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. 无需仅使用ID进行检查。

Hope my solution will help others who are stuck like me. 希望我的解决方案能够帮助像我一样受困的其他人。 Thanks to all for your efforts and helping hand. 感谢大家的努力和帮助。

just Replace Your code From this code 只需替换此代码中的代码

@Override
public void onClick(View v) {

    if (v == btn_save) 
        SaveSettings();
    else if (v == btn_close)
        CloseDialog();

    return;
}

to

@Override
public void onClick(View v) {

    switch(v.getId()){
            case R.id.saveBtn_settingDlg:
                SaveSettings();

            break;
            case R.id.closeBtn_settingDlg:
                CloseDialog();
            break;
        }
}

add this method in java class : 在java类中添加此方法:

public void CloseDialog(View v)
{

}

because in layout you have set android:onClick="CloseDialog" 因为在布局中您已经设置了android:onClick =“ CloseDialog”

Try this, I Hope it's of help 试试这个,希望对您有所帮助

 if(v.getId()==R.id.saveBtn_settingDlg)
       SaveSettings();

        else if (v.getId()==R.id.closeBtn_settingDlg)

            CloseDialog();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM