使用Android按钮切换到不同的Activity

时间:2018-03-01 20:25:00

标签: android android-activity init

我在屏幕上有按钮,当我按下屏幕上的按钮时,我想要进行不同的活动。

应用程序正在运行,但按钮停止。

请帮帮我。我正在学习android。抱歉英文不好。

我的班级档案;

public void git1(View view) {

    this.view = view;
    Intent intent1 = new Intent(getApplicationContext(),first.class);
    startActivity(intent1);

}

public void git2(View view) {

    this.view = view;
    Intent intent2 = new Intent(getApplicationContext(),second.class);
    startActivity(intent2);

}

public void git3(View view) {

    this.view = view;
    Intent intent = new Intent(getApplicationContext(),third.class);
    startActivity(intent);

}

public void git4(View view) {

    this.view = view;
    Intent intent = new Intent(getApplicationContext(),fourth.class);
    startActivity(intent);

}

public void git5(View view) {

    this.view = view;
    Intent intent = new Intent(getApplicationContext(),fifth.class);
    startActivity(intent);


} 

That's how the design looks.

我的XML文件;

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Anket Seçiniz :"
    tools:layout_editor_absoluteX="16dp"
    tools:layout_editor_absoluteY="16dp" />

<Button
    android:id="@+id/s1button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="45dp"
    android:onClick="git1"
    android:text="1. Hasta Memnuniyeti Anketi "
    android:textAlignment="textStart"
    app:layout_constraintBottom_toTopOf="@+id/s2button"
    app:layout_constraintStart_toStartOf="@+id/s2button" />

<Button
    android:id="@+id/s2button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/s1button"
    android:layout_marginTop="34dp"
    android:onClick="git2"
    android:text="2. Diş Bakımı Anketi "
    android:textAlignment="textStart"
    app:layout_constraintBottom_toTopOf="@+id/s3button"
    app:layout_constraintStart_toStartOf="@+id/s3button" />

<Button
    android:id="@+id/s3button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/s2button"
    android:layout_marginTop="25dp"
    android:onClick="git3"
    android:text="3. Türkiye Geneli Fenotip Anketi "
    android:textAlignment="textStart"
    app:layout_constraintStart_toStartOf="@+id/s4button"
    tools:layout_editor_absoluteY="231dp" />

<Button
    android:id="@+id/s4button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/s3button"
    android:layout_marginTop="31dp"
    android:onClick="git4"
    android:text="4. Yabani Hayvanlar Anketi"
    android:textAlignment="textStart"
    app:layout_constraintStart_toStartOf="@+id/s5button"
    tools:layout_editor_absoluteY="307dp" />

<Button
    android:id="@+id/s5button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/s4button"
    android:layout_marginTop="37dp"
    android:onClick="git5"
    android:text="5. ÜRÜN MEMNUNİYET ANKETİ"
    android:textAlignment="textStart"
    app:layout_constraintTop_toBottomOf="@+id/s4button"
    tools:layout_editor_absoluteX="31dp" />

1 个答案:

答案 0 :(得分:0)

例如:

@Override
public void onCreate(Bundle savedInstanceState) {
Button b1 = (Button) findViewById(R.id.buttonplay);       
b1.setOnClickListener(this);
//{YOUR APP}
}


@Override
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.buttonplay:
    //Play voicefile
    MediaPlayer.create(getBaseContext(), R.raw.voicefile).start();
    break;
case R.id.buttonstop:
    //Stop MediaPlayer
    MediaPlayer.create(getBaseContext(), R.raw.voicefile).stop();
    break;
 }

}}

我希望我帮助