2016-03-06 86 views
-1

我在Android Studio之前正在研究不同的代码网站,以及他们如何在第二个活动中打开按钮与此处不同。到目前为止,我有我的第二个活动按钮,它会打开..如何在第二个活动中打开多个按钮?

fifthactivity.java低于

Button button; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fifth_layout); 
    Button button = (Button) findViewById(R.id.button10); 
    button.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
      Intent intent = new Intent(FifthActivity.this, AmazonActivity.class); 
      FifthActivity.this.startActivity(intent); 
      } 
     }); 
    } 
} 

我明白我需要一个新的.java和新的布局引导按钮,我只需要帮助代码放入我的第五activity.java

下面是我的布局,我需要打开其他按钮。

<Button 
    tools:ignore="HardcodedText" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="PlayStation" 
    android:drawableLeft="@drawable/playstation" 
    android:drawableStart="@drawable/playstation" 
    android:layout_weight="0.07" 
    android:textSize="35sp" 
    android:id="@+id/button5" /> 
+0

我不确定我是否理解您的问题。你想打开你目前的活动吗? –

+0

@AmandaBrito我想从我的fifthactivity.java中的另一个按钮打开一个新的活动我已经有一个按钮打开“亚马逊”,但我有另一个按钮,我想要打开。 – Porzingis

+0

那么,从我的理解你想要在同一个布局中有几个按钮来打开多个屏幕?你可以改变你的布局,使其具有像RelativeLayout这样的容器,并且放置尽可能多的按钮。在你的活动中,你可以访问这些按钮并打开你想要的屏幕,就像你在上面发布的那样。这有帮助吗? –

回答

1
Button button; 
    Button anotherButton; // the second button OP required 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fifth_layout); 
     Button button = (Button) findViewById(R.id.button10); 
     anotherButton = (Button)findViewById(R.id.button5); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       Intent intent = new Intent(FifthActivity.this, AmazonActivity.class); 
       FifthActivity.this.startActivity(intent); 
       } 
      }); 

     /* new button to open a new activity */ 
     anotherButton.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View view) { 
       // creating the intent 
       Intent intent = new Intent(FifthActivity.this, AnotherActivity.class); 
       // starting activity with the created intent 
       startActivity(intent); 
       } 
      }); 
     } 
    } 
+0

就像这样,@Porzingis –

+0

@AmandaBrito完美!奇迹般有效!谢谢! – Porzingis

0

新按钮添加到您的XML文件和样式它你怎么样,并添加新的ID喜欢 android:id="@+id/button6"

<Button 
tools:ignore="HardcodedText" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="PlayStation" 
android:drawableLeft="@drawable/playstation" 
android:drawableStart="@drawable/playstation" 
android:layout_weight="0.07" 
android:textSize="35sp" 
android:id="@+id/button5" /> 

<Button 
android:id="@+id/button6" 
tools:ignore="HardcodedText" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:text="SomeText" 
android:textSize="35sp" /> 

在你fifthactivity.java添加新的按钮:

Button button, button2; 

并且像您之前的按钮一样,将点击侦听器添加到该按钮。使用它自己的布局创建新的Java类,并使用该按钮打开它,通过Intent

相关问题