2016-03-06 87 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

相關問題