基本的には、これに14個のボタンを追加する予定です。これらのボタンはすべて、2回目のアクティビティでスピナーの変更だけで同じアクティビティに移動します。だから、私は同じアクティビティにつながるすべてのボタンのインテントオブジェクトを作成したくはありません(アプリの速度が遅くなるため)。だから、私はidを取得するためにボタンにタグを割り当て、そのIDを新しいアクティビティに渡そうとしました。しかし、それは動作しません。 AVDは最初のアクティビティのみを表示し、ボタンをクリックしても何も起こりません。ボタンをクリックして別のアクティビティに切り替えることができない
マイメイン画面(Test1を)活性:
public class Test1Activity extends Activity {
public static final String pass="com.sanjay.test1._clicked";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button) findViewById(R.id.button1);
Button button2=(Button) findViewById(R.id.button2);
button1.setId(1);
button2.setId(2);
button1.setOnClickListener(new MyOnClickListener());
button2.setOnClickListener(new MyOnClickListener());
}
private class MyOnClickListener implements OnClickListener{
@Override
public void onClick(View v)
{
int id=v.getId();
Intent i=new Intent(Test1Activity.this,Sanjay.class);
i.putExtra(pass, id);
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Length" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Weight" />
</LinearLayout>
第二クラス(サンジェイ)活性:
public class Sanjay extends Activity {
/** Called when the activity is first created. */
int a;
TextView text;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
text=(TextView) findViewById(R.id.text);
a=getIntent().getExtras().getInt(Test1Activity.pass);
text.setText(" "+a);
// TODO Auto-generated method stub
}
}
Second.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
のAndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sanjay.rotation"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".RotationActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
あなたはインテントを作成しましたが、アクティビティを開始しませんでした。インテントメッセージでアクティビティを開始します。インテントi =新しいインテント(...); startActivity(i); – Jayabal