gpt4 book ai didi

java - 使用可点击图像的菜单?

转载 作者:行者123 更新时间:2023-12-01 09:46:53 26 4
gpt4 key购买 nike

我正在尝试使用可点击图像创建菜单,但它们似乎不起作用?我正在遵循一个教程,它使用类似

Intent biodata = new Intent(mainActivity.this, profile.class);

但它不起作用,所以我尝试寻找其他东西,有人说使用 v.getContext() 而不是 ....this

它在我的其他页面上有效,但在此页面上不起作用?

mainActivity.java

package skripsi.garden;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class mainActivity extends AppCompatActivity {

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

public ImageButton buttonBio;

public void init(){
buttonBio= (ImageButton)findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent biodata = new Intent(v.getContext(), profile.class);

startActivity(biodata);
}
}


);
}

public ImageButton buttonList;

public void tombollist(){
buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent listTaman = new Intent(v.getContext(), gardenlist.class);

startActivity(listTaman);
}
}


);
}

public ImageButton buttonWeather;

public void tombolcuaca(){
buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cuaca = new Intent(v.getContext(), weather.class);

startActivity(cuaca);
}
}
);
}
}

这是 xml Activity _main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="@drawable/foliagemain"
tools:context="skripsi.garden.mainActivity">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:src="@drawable/selamatdatang"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonBio"
android:src="@drawable/buttonbiodata"
android:layout_above="@+id/buttonList"
android:layout_alignRight="@+id/buttonWeather"
android:layout_alignEnd="@+id/buttonWeather" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonList"
android:src="@drawable/buttondaftartaman"
android:layout_above="@+id/buttonHelp"
android:layout_alignLeft="@+id/buttonHelp"
android:layout_alignStart="@+id/buttonHelp" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonHelp"
android:src="@drawable/buttonhelp"
android:layout_above="@+id/buttonWeather"
android:layout_alignLeft="@+id/buttonBio"
android:layout_alignStart="@+id/buttonBio" />

<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/buttonWeather"
android:src="@drawable/buttoncuaca"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />



</RelativeLayout>

最佳答案

将按钮放入 onCreate 方法中。他们不需要使用自己的方法。

public class mainActivity extends AppCompatActivity {

public ImageButton buttonBio;
public ImageButton buttonList;
public ImageButton buttonWeather;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


buttonBio= (ImageButton) findViewById(R.id.buttonBio);
buttonBio.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent biodata = new Intent(mainActivity.this, profile.class);
startActivity(biodata);
}
});

buttonList=(ImageButton)findViewById(R.id.buttonList);
buttonList.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent listTaman = new Intent(mainActivity.this, gardenlist.class);
startActivity(listTaman);
}
});

buttonWeather=(ImageButton)findViewById(R.id.buttonWeather);
buttonWeather.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cuaca = new Intent(mainActivity.this, weather.class);
startActivity(cuaca);
}
});
}
}

关于java - 使用可点击图像的菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37927930/

26 4 0