-1

ユーザーがフィールドに入力する最初のアクティビティと、最初のアクティビティでボタンを押して起動する必要がある2番目のアクティビティで構成されるEclipseでAndroidアプリケーションを作成しようとしています。

インテントに関する多くのトピックを読み、インテント フィルターに関するすべての可能な情報を受け取りましたが、ボタンを作成してインテントを使用してアクティビティを起動することはまだできません。

助けてください。

それを行うための段階的な指示のようなものがあれば、それは素晴らしいことです.

わかりました、これがあなたの答えに従って私が作ったものです->

アプリケーションを作成しました。src には 4 つの Java ファイルがあります:
Activity1.java - Activity2 を起動するボタンが必要なアクティビティ、
Activity2.java - これはデフォルト状態、
MyActivity.java - 1Up で受信したコードを貼り付けるために作成された、
View.java -私が作成した空のクラスは、それがないと多くのエラーが表示されるため、通常はそれらを削除する方法がわかりません。

次に、1Up のコードを MyActivity に入れ、少し編集したところ、次のようになりました。

public class Activity1<i> extends Activity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity1, menu);
    return true;
}
public void onCreate() {
    // TODO Auto-generated method stub

}

    public void goToOtherActivity(View view)
    {
        Intent i = new Intent(this, Activity2.class);
        this.startActivity(i);
    }                                        }

次に、Activity1のonClickにgoToOtherActivityをアタッチできると思ったのですが、テキストのようにしか追加できなかったので、AVDでアプリを起動すると多くの警告が表示されます

4

3 に答える 3

2

何らかのイベントがトリガーされたときに(クリック時に)新しいアクティビティを開始したい場合を考えてみましょう

たとえば、ここにXMLレイアウトがあります

<LinearLayout
        android:id="@+id/start_btn"
        android:layout_width="75.0dip"
        android:layout_height="27.0dip"
        android:layout_marginRight="35.0dip"
        android:background="@drawable/green_btn"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingBottom="2.0dip"
        android:paddingLeft="8.0dip" >

        <ImageView
            android:id="@+id/start_image"
            android:layout_width="9.0dip"
            android:layout_height="14.0dip"
            android:src="@drawable/start_icon" />

        <TextView
            android:id="@+id/start_caption"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5.0dip"
            android:gravity="center_vertical"
            android:text="Start Activity"
            android:textColor="#ffffffff"
            android:textStyle="bold" />
    </LinearLayout>

Intent新しいアクティビティを開始するために使用できます

LinearLayout lBtn = (LinearLayout) findViewById(R.id.start_btn);
lBtn.setOnClickListener(new View.OnClickListener() {
     @Override public void onClick(View arg0) {
     Intent mAct = new Intent(this, ActivtyClass.class);
     startActivity(mAct);
    } });
于 2013-09-15T12:58:23.493 に答える