用于两个动作一扣 [英] One button used for two Action

查看:117
本文介绍了用于两个动作一扣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何一个可以告诉我吗?我怎么可以使用相同的按钮两行动?
就像在我的活动,计算一些数值,计算当我再次preSS相同的按钮,然后重新设置所有字段后,使用EM一个按钮。像在本申请
http://www.craziness.com/games/play-love-tester/当我测试由pressing按钮的爱
然后我再preSS同一按钮,然后重置所有字段。
我应该在我的活动中使用的上述问题?

Can any one tell me? how can i use the same button for two Action? Like em using one button in my activity that calculate some values and after calculating the when i again press same button then reset the all fields. Like in this Application http://www.craziness.com/games/play-love-tester/ when i test the love by pressing the button then i again press the same button then all fields reset. what should i use in my activity for the above problem?

推荐答案

您可以创建一个全局变量,表示程序的状态,然后在需要时更改此值。在 OnClickListener 你的按钮的创建一个如果语句它检查这个变量,并做必要的事情相关的值。

You can create a global variable which indicates the state of the program and then change this value when needed. In the OnClickListener of your Button you create an if statement which checks this variable and does the needed things for the associated value.

例如:

public class MainActivity extends Activity {
    private int state = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (state == 0) {
                    // State 1
                }
                else if (state == 1) {
                    // State 2 
                }
                else {
                    // Default state
                }
            }
        });

        // Rest of your code including state changing part
    }
}

这篇关于用于两个动作一扣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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