Android: Button click event

前提是你 提交于 2019-11-29 03:42:05
Nguyen Minh Binh

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

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

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.

Button Name is MyButton.it's working.

   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 :

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

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.

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.

add this method in java class :

public void CloseDialog(View v)
{

}

because in layout you have set android:onClick="CloseDialog"

Kiran Babu

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();
Ravi Makvana

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;
        }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!