2014-11-20 65 views
-1

我想知道你們是否可以幫助我在我的應用程序。這對我們錯過的你們來說真的很容易。我試圖鏈接一個佈局上的按鈕以導航到其他佈局。這六個按鈕應該去他們的六種不同的佈局...如何將這些按鈕鏈接到其佈局?

按鈕7應該去佈局編號7 我已經做了按鈕1,它的工作原理爲number1。

這裏的佈局的屏幕截圖,這裏是我的代碼我main.java:http://imgur.com/zLJ3cdE

package com.example.isthisyourluckyday; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Main extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
      final Button b = (Button) findViewById(R.id.button1); 

      b.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
         // TODO Auto-generated method stub 
         startActivity(new Intent(Main.this, Number1.class)); 

       } 
      }); 
    } 
} 

而且......這裏是number7.java應該鏈接到number7佈局

package com.example.isthisyourluckyday; 

import android.app.Activity; 
import android.os.Bundle; 
import android.widget.Button; 

public class Number7 extends Activity { 
              Button button7; 
              @Override 
              protected void onCreate(Bundle savedInstanceState) { 
                // TODO Auto-generated method stub 
                super.onCreate(savedInstanceState); 
                setContentView(R.layout.number7); 
}} 

如果你們可以請幫助我,我真的很感激它。

+0

它太有霧,我不能得到你所需要的。你會冷靜下來,清楚地解釋你想要什麼嗎? – 2014-11-20 18:01:26

+0

我想你想鏈接到這些佈局的新活動,對吧? – 2014-11-20 18:02:15

回答

0

如果第一個按鈕有效,其他人的問題在哪裏?

下面的所有代碼:

public class Main extends Activity implements View.OnClickListener { 

private final Button button1; 
private final Button button2; 
private final Button button3; 
private final Button button4; 
private final Button button5; 
private final Button button6; 
private final Button button7; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    button1 = (Button) findViewById(R.id.button1); 
    button2 = (Button) findViewById(R.id.button2); 
    button3 = (Button) findViewById(R.id.button3); 
    button4 = (Button) findViewById(R.id.button4); 
    button5 = (Button) findViewById(R.id.button5); 
    button6 = (Button) findViewById(R.id.button6); 
    button7 = (Button) findViewById(R.id.button7); 

    button1.setOnClickListener(this); 
    button2.setOnClickListener(this); 
    button3.setOnClickListener(this); 
    button4.setOnClickListener(this); 
    button5.setOnClickListener(this); 
    button6.setOnClickListener(this); 
    button7.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    if(v.equals(button1)) 
     startActivity(new Intent(Main.this, Number1.class)); 
    else if(v.equals(button2)) 
     startActivity(new Intent(Main.this, Number2.class)); 
    else if(v.equals(button3)) 
     startActivity(new Intent(Main.this, Number3.class)); 
    else if(v.equals(button4)) 
     startActivity(new Intent(Main.this, Number4.class)); 
    else if(v.equals(button5)) 
     startActivity(new Intent(Main.this, Number5.class)); 
    else if(v.equals(button6)) 
     startActivity(new Intent(Main.this, Number6.class)); 
    else if(v.equals(button7)) 
     startActivity(new Intent(Main.this, Number7.class)); 

} 
} 

主類可以實現OnClickListener到只有一個的onClick()函數。

0

我沒有正確測試,但你明白了。它與其他答案類似,但在我看來,代碼更簡潔,重複性更低。

package com.example.isthisyourluckyday; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class Main extends Activity implements View.OnClickListener { 

    final Button[] buttons = new Button[7]; 
    final Class[] classes = { Number1.class, Number3.class, Number3.class, 
      Number4.class, Number5.class, Number6.class, Number7.class }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(final Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     buttons[0] = (Button) findViewById(R.id.button1); 
     buttons[1] = (Button) findViewById(R.id.button2); 
     buttons[2] = (Button) findViewById(R.id.button3); 
     buttons[3] = (Button) findViewById(R.id.button4); 
     buttons[4] = (Button) findViewById(R.id.button5); 
     buttons[5] = (Button) findViewById(R.id.button6); 
     buttons[6] = (Button) findViewById(R.id.button7); 

     for(Button b : buttons){ 
      b.setOnClickListener(this); 
     } 
    } 

    @Override 
    public void onClick(View v) { 
     for(int i=0; i<buttons.length; i++){ 
      if(buttons[i].equals(v)){ 
       Intent intent = new Intent(Main.this, classes[i]); 
       startActivity(intent); 
      } 
     } 
    } 
} 
0

當你開始喜歡這個 startActivity(新意圖(Main.this,Number1.class))的活動; 您正在告訴Android啓動Number1活動。所以,如果你想開始另一個活動,你只要做你做同樣的事情:

final Button b7 = (Button) findViewById(R.id.button7);  
b7.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
    startActivity(new Intent(Main.this, Number7.class)); 
    } 
}); 

這不是優雅,但會做的伎倆。

您也可以定義每個按鈕「標籤」像

<Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:tag="1" 
     android:onClick="choose"/> 

然後在您的活動添加一個方法

public void choose(View view) { 
     String number = view.getTag().toString(); 
     Intent intent=new Intent(); 
     intent.setComponent(new ComponentName("com.example.isthisyourluckyday", "com.example.isthisyourluckyday.Number"+number)); 
     startActivity(intent); 
    }