gpt4 book ai didi

java - 我无法通过单击按钮启动 Activity

转载 作者:行者123 更新时间:2023-12-01 20:06:43 25 4
gpt4 key购买 nike

我正在尝试通过单击按钮来启动 Activity 。如果用户单击 MenuActivity 中的 PLAY 按钮,则应该启动 GameActivity。但这不起作用。

public class MenuActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
}

public void startGameEngine(View view) {
Intent myintent = new Intent(this, GameActivity.class);
startActivity(myintent);
}

这是 GameActivity 类,当用户单击“游戏”按钮时应启动该类。

public class GameActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Intent intent = getIntent();
}

这是包含按钮的 menu.xml 文件

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/menubackground_color"
tools:context="com.example.vincenzoauricchio.example.MenuActivity">

<TextView
android:id="@+id/textView3"
android:layout_width="381dp"
android:layout_height="67dp"
android:layout_marginBottom="440dp"
android:layout_marginTop="88dp"
android:fontFamily="@font/vt323"
android:text="@string/logo2"
android:textAlignment="center"
android:textAllCaps="false"
android:textColor="@android:color/black"
android:textSize="36sp"
android:textStyle="bold|italic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.666"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/btnPlay"
android:layout_width="100dp"
android:layout_height="60dp"
android:layout_marginBottom="265dp"
android:layout_marginTop="87dp"
android:fontFamily="@font/vt323"
android:onClick="startGameEngine"
android:text="@string/button_play"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView3"
app:layout_constraintVertical_bias="0.0" />

最佳答案

尝试实现点击监听器:

public class MenuActivity extends AppCompatActivity implements View.OnClickListener

尝试下面的代码 fragment :

Button btnPlay = (Button) findViewById(R.id.btnPlay);
btnPlay.setOnClickListener(MainActivity.this);

@Override
public void onClick(View view)
{
if(view == btnPlay)
{
Intent intent = new Intent(MenuActivity.this, GameActivity.class);
startActivity(intent);
}
}

将您的 GameActivity 添加到 list 中。

关于java - 我无法通过单击按钮启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47364661/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com