在意图之间进行选择(按钮单击) - Android 应用程序开发

发布于 2024-12-05 11:33:14 字数 1829 浏览 0 评论 0原文

我是 Android 应用程序开发新手,一直在开发一个简单的 FlashLight 应用程序,可以在屏幕上的各种颜色之间切换。初始活动绘制了一个只有 2 个按钮的布局,分别标记为绿色和蓝色。我在两个按钮上安装了单击监听器,并在它们上设置了意图,以便每个按钮加载其相应的活动,但是当我运行应用程序时,我只能从第一个视图转到其他视图之一(绿色或蓝色,但不是两者都)。我希望能够选择任一按钮并加载下一个活动,但我有点迷失了。也许创建一个布尔值来确定用户单击了哪个按钮?我不知道。这听起来可能有点令人困惑,因为我不擅长描述这样的技术问题,但下面是我的代码。

package com.jbsoft.SimpleFlashlight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
import android.widget.Toast;

public class SimpleFlashLightActivity extends Activity {

  Button GreenButton;
  Button BlueButton;


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    BlueButton = (Button) findViewById(R.id.bluebutton);
    BlueButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {

        Intent blueintent = new Intent(SimpleFlashLightActivity.this,
                                       BlueFlashLightActivity.class);
        startActivity(blueintent);

        Toast.makeText(v.getContext(), "SWITCH COLOR!",
                       Toast.LENGTH_LONG);

        GreenButton = (Button) findViewById(R.id.bluebutton);
        GreenButton.setOnClickListener(new View.OnClickListener() {

          public void onClick(View v) {

            Intent greenintent = new Intent(
              SimpleFlashLightActivity.this,
              GreenFlashLightActivty.class);
            startActivity(greenintent);

          }
        });
      }
    });

  }

  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu);
    return true;
  }

}

I am new to Android application development, and I've been working on a simple FlashLight app that switches between various colors on the screen. The initial activity draws up a layout with only 2 buttons, labeled Green and Blue. I've installed Click Listeners on both buttons and set intents on them both in order for each to load it's corresponding activity, but when I run the app I can only go from the first view to ONE of the other views(Green OR Blue, but not both). I want to be able to choose EITHER button and load the next activity, but I'm a bit lost. Maybe create a boolean that determines which button the user clicked? IDK. This may sound a bit confusing as I'm not good at describing technical things like this, but here's my code below.

package com.jbsoft.SimpleFlashlight;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.Button;
import android.widget.Toast;

public class SimpleFlashLightActivity extends Activity {

  Button GreenButton;
  Button BlueButton;


  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    BlueButton = (Button) findViewById(R.id.bluebutton);
    BlueButton.setOnClickListener(new View.OnClickListener() {

      public void onClick(View v) {

        Intent blueintent = new Intent(SimpleFlashLightActivity.this,
                                       BlueFlashLightActivity.class);
        startActivity(blueintent);

        Toast.makeText(v.getContext(), "SWITCH COLOR!",
                       Toast.LENGTH_LONG);

        GreenButton = (Button) findViewById(R.id.bluebutton);
        GreenButton.setOnClickListener(new View.OnClickListener() {

          public void onClick(View v) {

            Intent greenintent = new Intent(
              SimpleFlashLightActivity.this,
              GreenFlashLightActivty.class);
            startActivity(greenintent);

          }
        });
      }
    });

  }

  public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    MenuInflater mi = getMenuInflater();
    mi.inflate(R.menu.list_menu, menu);
    return true;
  }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃不饱 2024-12-12 11:33:14

您正在 bluebotton 的 onclick 事件中为 greenbutton 设置 onclicklistener。因此,只有当用户单击 bluebotton 时才会设置侦听器。或者我在这里遗漏了什么?

在蓝色按钮的列表之外设置绿色按钮的 onclick 监听器,它应该可以工作。

you are setting the onclicklistener for the greenbutton in the onclick event of the bluebotton. so the listener gets set only once the user clicks the bluebotton. or am i missing something here?

set the onclick listener for the greenbutton outside of the listnere for the bluebutton and it should work..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文