Android - to control the button from another activ

2019-05-24 13:13发布

I have two activities.

Home activity contain a list view with two buttons named checkIn and directions.when checkIn button is clicked it does some operation (say A).

When direction button is clicked it launches directions activity.So in directions activity if some condition satisfies there comes an alertbox asking for whether to check In or not.If yes is clicked,I want to do operation A on the checkin button but without destroying directions activity.ie,I want to control the button OnclickListener with the status of an alertbox in other activity without losing the state of present activity.

this is the portion of the code in getView of Listadapter used by Listview of Home activity.btnChild1 is the CheckIn button.

btnChild1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (btnChild1.getText().toString().equals("Check In")) {
                btnChild1.setText("Cancel");
                taskSubList.get(position).setCheckIn(1);
                startTime = System.currentTimeMillis();

            } else {
                btnChild1.setText("Check In");
                taskSubList.get(position).setCheckIn(0);
                long difference =taskSubList.get(position).getTimeSpent() +System.currentTimeMillis() - startTime;
                taskSubList.get(position).setTimeSpent(difference); 
                String values[]={Integer.toString(taskSubList.get(position).getId()), Integer.toString((int) difference)};
                String updateTime=Helper.getfromUrl(updateTimeUrl,values);
                if (!updateTime.equals("success"))
                {
                    Toast.makeText(context, "Not updated", Toast.LENGTH_SHORT).show();
                }
                Intent reasonIn = new Intent(context.getApplicationContext(), Reason.class);
                context.startActivity(reasonIn);
            }

        }
    }); 

And in Directions.java

    if((int)distance/1000 <= 30 && checkInStatus == 0)
    {
        AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
        alertbox.setMessage("Do you want to Check In?");
        alertbox.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // I have to do the above function
            }
        });
       alertbox.setNegativeButton("No", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {

            }

        });
        alertbox.show();
    }

3条回答
兄弟一词,经得起流年.
2楼-- · 2019-05-24 13:40

What you could do is write a checkIn function and tie both of these buttons to it, it all depends on what you need to do. This way you create a utility class that handles the checkIn etc. but tie both the button in activities.Home and activities.Directions to operation A in the utility class.

查看更多
霸刀☆藐视天下
3楼-- · 2019-05-24 13:44

"without destroying directions activity" - Just dont call finish() method and the activity will not be destroyed but will be hold only "onPause".

In your situation you need to be more specific what the checkln stuff does. if it somehow changed the UI of the first activity or not for instance.

If the checkln just display something (image, text, data from web service) just call

StartActivityForResult(Intent intent, int requestCode);

and then when the time for displaying infromation is fullfied just finish the newly created activity and call

SetResult(int resultCode);

and you are back to your second activity displaying the same what it was displaying before the alertbox was shown.

查看更多
淡お忘
4楼-- · 2019-05-24 13:46

What you have to do is:

Import the needed library:

import android.app.Activity;

On your secondary activity, you define:

public static Activity myActivity;

Then, on your main activity, you say:

SecondaryActivityName.myActivity=this;

Finally, when you want to call that button of the main Activity from the secondary one:

Button myButton=(Button) SecondaryActivityName.myActivity.findViewById(R.id.ButtonID);
myButton.setOnClickListener(this);
查看更多
登录 后发表回答