我正在尝试在Android Studio中编写游戏代码。我运行apk,但是按钮什么也没做。
当单击“ oyna”(表示播放)或“ ayarlar”(表示设置)按钮时,游戏设置或设置类应运行,但什么也没有发生。简要地说,按钮不响应。我能做什么 ?
public class MainActivity extends AppCompatActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button settings = (Button)findViewById(R.id.settings_button);
Button play = (Button) findViewById(R.id.play_button);
assert play != null;
assert settings != null;
settings.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent settings = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(settings);
}
});
play.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, GameSettingsActivity.class);
startActivity(intent);
}
});
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:foreground="@mipmap/anasayfa3"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/play_button"
android:layout_width="128dp"
android:layout_height="67dp"
android:layout_marginEnd="283dp"
android:layout_marginRight="283dp"
android:layout_marginBottom="388dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/settings_button"
android:layout_width="176dp"
android:layout_height="61dp"
android:layout_marginStart="244dp"
android:layout_marginLeft="244dp"
android:layout_marginBottom="296dp"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
答案 0 :(得分:1)
两个按钮都已在xml中设置为不可见。
请记住,不可见的视图不会收到触摸事件。 但是,如果您确实希望按钮不可见并且仍然对触摸事件做出反应,则可以使用一些替代方法。
将视图的Alpha设置为0。这将使其完全透明,并且仍将监听单击/触摸事件。
在父级上放置触摸处理程序,并检查触摸是否在不可见视图的区域中
第一个更容易做到。