ページ上に複数のボタンをリンクすることはできません。 ページには、基本的に見出しを持っており、3種類の話題 私がしようとしているコード(私は、このサイトにある)にリンクされている以下の3つのボタンは:androidのボタンをリンクする
package com.ICTrevisionapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class topicstoquiz extends Activity {
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.topics);}
public void onClick(View v) {
{
Button clickedButton = (Button) v;
setContentView(0);
switch(clickedButton.getId())
{
case R.id.button2:
setContentView(R.layout.topic1);
Intent myIntent = new Intent (v.getContext(),topicstotopicone.class);
startActivityForResult(myIntent, 0);
break;
case R.id.button3:
setContentView(R.layout.topic2);
break;
}
}
}
ケース部。
私はまた、コード試してみました:
Intent myIntent = new Intent (view.getContext(),topicstoquiz.class);
startActivityForResult(myIntent, 0);
をしかし、私はそれが一つの活性に一つだけのボタンからリンクする得るように見えることができます。
私はおそらくこれを完全に間違っているので、別のページにリンクできるように、ページ上の各ボタンを個別のアクティビティにリンクするにはどうすればよいでしょうか。あなたのonCreateで
:
private OnClickListener yourListener = new OnClickListener(){
public void onClick(View v){
Intent yourIntent = new Intent(yourCurrentActivity.this, classYoureNavigatingToo.class);
startActivity(yourIntent);
}
};
:
Button button2 = (Button)findViewById(R.id.button2);
button2.setOnClickListener(yourListener);
が続いてリスナーメソッドを作成
Imが失われたが、追加しますか? super.onCreate(savedInstanceState); または protected void onCreate(Bundle savedInstanceState) また、私もそこにあるケースステートメントを取り除く必要がありますか? クイック返信ありがとうございます:) – Crazyfool
メソッドonCreate。 super.onCreate ...はスーパーコールだけです。あなたはあなたのsetContentView呼び出しの後に投稿したコードの最初の部分を置くでしょう。 – bschultz
はい、あなたは私が提示した方法でそれについて行く場合は、ケースステートメントを取り除くだろう。リフトされたnifftyRichサンプルも、あなたが探している結果を提供します。 – bschultz