【问题标题】:lauching different activities depending on a value根据价值启动不同的活动
【发布时间】:2013-05-18 15:04:31
【问题描述】:

当用户单击下一个按钮时,我正在尝试根据参数的值启动一个新活动

    <Button
    android:id="@+id/next"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/next" />

我认为我不应该使用我知道的代码来启动一个新活动,因为它只启动一个活动。

android:onClick="lanzarActividad"

我的目的是用户设置一个参数(一个数字),每个数字对应不同的活动。

我想我应该写一些类似的东西(在 onCreate 方法中)

Button btn=(Button)findViewById(R.id.next);
int parameter=1;
//it already contains a number, in our case, it might be for instance 1
    btn.setOnClickListener(new View.OnClickListener() 
    {
        public void onClick(View v)
        {
            switch(parameter)
            {
            //Now I get the problem.
               case 0 : 
               lanzarCalculate(v);

               case 1 :
               lanzarCalculate2(v);
            }
        }
    });

创建意图的方法是

public void lanzarCalculate(View view)
//si apretamos el boton calculate
{
    Intent i = new Intent(this, Calculate.class);
    Bundle b = new Bundle();
    b.putDouble("time", tau);
    i.putExtras(b);
    startActivity(i);
}

public void lanzarCalculate2(View view)
{
    Intent i = new Intent(this, Calculate2.class);
    Bundle b = new Bundle();
    b.putDouble("time", tau);
    i.putExtras(b);
    startActivity(i);
} 

在此处输入代码

非常感谢你,希望你能帮助我。

【问题讨论】:

  • @user2374720 请告诉你遇到了什么问题。
  • 您是在告诉我们您对自己的行为不安全吗?你为什么不试试你的方法,这不像你要制造核弹:)
  • 我不知道如何启动活动。我的意思是,我不知道 case x 之后的 java 代码:我尝试使用以下代码来执行此操作(调用启动新活动的方法),但它不起作用code switch(option ) { //现在我得到了问题。案例 0:launchActivity1(v);案例1:launchActivity2(v); }
  • 你应该在一开始就告诉我们:)
  • 对不起,我快疯了,我不知道为什么我的程序在调用这些方法时会退出:S

标签: android button android-activity


【解决方案1】:

我认为您的问题是您不确定如何“动态”使用 Intent。我就是这样做的,以避免重复代码

@Override
    public boolean onMenuItemClick(MenuItem item) 
    {
        Intent intent = new Intent();        // first, create your Intent object
        String nextAct = null;               // name for Activity to start with Intent
        String package = "com.my.package.";  // set package name
        int flag = -1;                      // in case you want to set certain flags depending on activity

        switch (item.getItemId())
        {
            case (R.id.logout):            // mine are menu options so you will use the ints that you have already
                nextAct = package + "LoginScreen";
                flag = Intent.FLAG_ACTIVITY_CLEAR_TOP;
                break;
            case (R.id.changeLocation):
                nextAct = package+ "ChangeLocation";   // package is reused but in some cases it different for me
                break;      
            default:
                Toast.makeText(getApplicationContext(), "Item currently not available", Toast.LENGTH_SHORT).show();
        }

        try 
        {
            if (nextAct != null)
            {
                intent = new Intent(BaseActivity.this, Class.forName(nextAct));  // change the String that I was setting to the Activity name
                if (flag != -1)
                {   intent.setFlags(flag);  }  // set my flags if I had any
                startActivity(intent);  // start the Activity as you normally would
            }

这是用于 PopupMenu,但您可以在 onClick() 中执行相同的操作。我在代码中添加了 cmets 来帮助解释它的作用。

还有,这个

android:onClick="lanzarActividad"

在您的 xml 中会很好。有了这个,你就不需要定义你的 Button 和监听器

public void lanzarActividad(View v)
{
    // code from above
}

当你点击你的Button时,这个函数将被调用并运行

编辑

您的潜在问题似乎是您在创建Intents 时需要使用YourActivityName.this 而不是this。此外,在每种情况下添加break; 语句,否则它将继续运行下一个case。但是上面的代码会让你不需要重写Intent的代码,因为大部分都是一样的

【讨论】:

  • 我以前做过....但它不起作用....请看一下这个问题(我已经添加了我尝试做的事情)跨度>
【解决方案2】:

如果您搜索“开始活动”http://developer.android.com/training/basics/firstapp/starting-activity.html,文档中对此进行了很好的解释

基本上你需要创建一个Intent,然后以intent为参数调用startActivity方法,例如:

Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);

有关更多选项和示例,请参阅文档页面。

【讨论】:

  • 我以前做过....但它不起作用....请看一下这个问题(我已经添加了我尝试做的事情)跨度>
  • 您是否在清单中声明了CalculateCalculate2?你能粘贴你在 logcat 上得到的异常吗?
猜你喜欢
  • 2014-10-06
  • 1970-01-01
  • 2011-04-05
  • 2012-04-15
  • 1970-01-01
  • 2017-06-04
  • 2011-02-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多