在Android中6秒后启动按钮OnClick操作

时间:2016-07-17 20:48:12

标签: android android-button

我的活动中有5个按钮,位于线性布局中。 当我单击其中一个按钮时,我已经编写了一个显示Toast消息的代码。 它在点击操作6秒后显示Toast消息。 我想不出有什么问题.. 这是我在android studio中编写的代码

public class HomePage extends AppCompatActivity implements View.OnClickListener {

     private Button loginButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);

        makeNotificationBarTransparent();

        loginButton = (Button)findViewById(R.id.login_btn);

        loginButton.setOnClickListener(this);





        Intent i = getIntent();
        Toast.makeText(getApplicationContext(),i.getStringExtra("UserName"),Toast.LENGTH_LONG).show();
    }

    private void makeNotificationBarTransparent() {
        //Making notification bar transparent
        if(Build.VERSION.SDK_INT >= 21){
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.login_btn:
                Toast.makeText(getApplicationContext(),"CLicked",Toast.LENGTH_LONG).show();
                break;
        }
    }
}

谁能告诉我可能是什么问题?

2 个答案:

答案 0 :(得分:2)

可能是您显示另一个设置为显示的Toast消息,因为无法一次显示两个Toast消息。

我指的是这个祝酒词:

        Toast.makeText(getApplicationContext(),i.getStringExtra("UserName"),Toast.LENGTH_LONG).show();

答案 1 :(得分:0)

你的意思是单击按钮6秒后会显示Toast消息? 试着用这个:

class player:

    def __init__(self, n):
        self.name = n
        self.inventory = []
        self.health = 10.0

    def getName(self):
        return self.name

    def printName(self):
        print("Your name is: " + self.name)

    def printInventory(self):
        print(self.inventory)


class game:

    def __init__(self):
        print("Welcome to Choose Your Adventure.")
        name = input("Please enter your name to begin: ")
        p = player(name)

    def intro(self):
        print("\n.....\n")
        ans = input("You awaken in a field skirted by a dense pine forest.\n" +
              "A rickety barn and its adjoining house lie a few hundred\n" +
              "feet ahead of you. Do you enter the forest or explore the\n" +
              "property? Type 'property' or 'forest': ")
        return ans

    def property(self):
        print("\n.....\n")
        print("You walked towards the property")

    def forest(self):
        print("\n.....\n")
        print("You walked into the forest")

    ###

    def unnamedMethod(self, m, ans1, ans2):
        ans = m() #where the error message occurs
        while ans.lower() != ans1 and ans.lower() != ans2:
            print("Please submit a valid response.")
            print("\n.....\n\n")
            ans = m()
        if ans.lower() == ans1:
            return ans1
        else:
            return ans2


class run:

    def __init__(self):
        g = game()
        print(g.unnamedMethod(g.intro(), "property", "forest"))


r = run()

或者您可以使用CountDownTimer:

Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
           @Override
           public void run() {
               // show toast here...
           }
    }, 6000); // 6 seconds

这是你需要的吗?如果没有,请告诉我有关您的问题的更多信息:)