Android how to pass an Activity.class as an argument for a function

后端 未结 7 2258
南方客
南方客 2021-02-12 12:22

I recently moved to Android from python and am stuck here.

This is my class declaration to create a common function for an Alert Dialog which accepts necessary parameter

7条回答
  •  离开以前
    2021-02-12 12:52

    call like this:

    ButtonClickBySani(R.id.btnsehrabandi, sehrabandiActivity.class);
    

    Definition:

    private void ButtonClickBySani(int ButtonId, final Class ActivityToOpen)
    {
        Button btn;
        // Locate the button in activity_main.xml
        btn = (Button) findViewById(ButtonId);
    
        // Capture button clicks
        btn.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                startActivity(new Intent(getBaseContext(), ActivityToOpen));
                // Start NewActivity.class
                //Intent myIntent = new Intent(getBaseContext(), ActivityToOpen);
               // startActivity(myIntent);
            }
        });
    }
    

提交回复
热议问题