Android-控制其他活动中的按钮 [英] Android - to control the button from another activity

查看:147
本文介绍了Android-控制其他活动中的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动.

家庭活动包含一个列表视图,该列表视图包含两个分别名为 checkIn 路线的按钮.单击checkIn按钮时,它会执行某些操作(例如A).

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

当单击方向按钮时,它将启动方向活动.因此,在方向活动中,如果满足某些条件,则会出现一个警报框,询问是否签入.如果单击是,我要对签入按钮执行操作A,但是不破坏方向活动.即,我要控制按钮OnclickListener的状态为其他活动中的警报框,而又不丢失当前活动的状态.

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.

这是Home活动的Listview使用的Listadapter的getView中的代码部分.btnChild1是签入"按钮.

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

        }
    }); 

在Directions.java中

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

推荐答案

不破坏指示活动"-只需不调用finish()方法,该活动就不会被破坏,而只会保留"onPause".

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

在您的情况下,您需要更具体地检查checkln的功能.例如,是否以某种方式更改了第一个活动的用户界面.

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.

如果checkln仅显示某些内容(来自Web服务的图像,文本,数据),请致电

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.

这篇关于Android-控制其他活动中的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆