简体   繁体   English

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

[英]Switch to different Activity with buttons Android

I have buttons on a screen, I want to go through different activities when I push the buttons on the screen.我在屏幕上有按钮,当我按下屏幕上的按钮时,我想进行不同的活动。

the application is working but the button stops.应用程序正在运行,但按钮停止。

Please, help me.请帮我。 I'm learning android.我正在学习安卓。 Sorry bad English.抱歉英语不好。

My class file;我的班级档案;

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.这就是设计的样子。


My XML file;我的 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" />

For example:例如:

@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;
 }

}}

I hope I've helped我希望我有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM