您好我有一個layout.xml與3個按鈕鏈接到單獨的佈局。使用意圖的android eclipse
我設法使用意圖編碼爲1按鈕。但是,我不知道如何在下一個按鈕中添加它們,以便它們分別佈局。這是我的代碼。
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 Activity1 extends Activity implements OnClickListener {
Button hello1, hello2, hello3;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hello1 = (Button)findViewById(R.id.hello1);
hello2 = (Button)findViewById(R.id.hello2);
hello3 = (Button)findViewById(R.id.hello3);
hello1.setOnClickListener(this);
hello2.setOnClickListener(this);
hello3.setOnClickListener(this);
}
public void onClick(View src) {
Intent hello1 = new Intent(this, Hello1Activity.class);
startActivity(hello1);
Intent hello2 = new Intent(this, Hello2Activity.class);
startActivity(oltp);
Intent hello3 = new Intent(this, Hello3Activity.class);
startActivity(oltp);
}
}
這根本不起作用..點擊第一個按鈕轉到hello3。如果我刪除hello2和hello3,那麼hello1運作良好。任何想法請。
然而,謝謝你,如果你能改變我的上面的代碼和你的答案,我真的很感激,因爲我不知道該把這個放在哪裏..如果你能詳細說明我的代碼,我失去了遺憾。 – neweclipseuser 2011-05-24 06:30:14
我已經使用你的代碼來讓它爲第一個按鈕工作。您可以複製此代碼並將「1」替換爲「2」和「3」以使其爲其他兩個按鈕起作用。您必須將此代碼放置在您的活動的onCreate方法中(無論您現在擁有hello1.setOnClickListener(this))。讓我知道如果這不明確。 – Kalarani 2011-05-24 08:36:39
謝謝,我嘗試過,但無法讓它解決..它給了一個強制性的錯誤。我不知道如何向你展示代碼,因爲我無法適應它。:(:( – neweclipseuser 2011-05-24 12:06:22