How to create our own Listener interface in android?

前端 未结 9 2078
不知归路
不知归路 2020-11-22 04:55

Could someone help me to create user defined listener interface with some code snippets?

9条回答
  •  遥遥无期
    2020-11-22 05:37

    Simple method to do this approach. Firstly implements the OnClickListeners in your Activity class.

    Code:

    class MainActivity extends Activity implements OnClickListeners{
    
    protected void OnCreate(Bundle bundle)
    {    
        super.onCreate(bundle);    
        setContentView(R.layout.activity_main.xml);    
        Button b1=(Button)findViewById(R.id.sipsi);    
        Button b2=(Button)findViewById(R.id.pipsi);    
        b1.SetOnClickListener(this);    
        b2.SetOnClickListener(this);    
    }
    
    public void OnClick(View V)    
    {    
        int i=v.getId();    
        switch(i)    
        {    
            case R.id.sipsi:
            {
                //you can do anything from this button
                break;
            }
            case R.id.pipsi:
            {    
                //you can do anything from this button       
                break;
            }
        }
    }
    

提交回复
热议问题