更新:
我有 4 个图像按钮,每个按钮都需要一个“setOnClickListener”或类似的按钮。监听器触发后,我打算将用户带到下一个 Activity,该 Activity 当前只是一个空白页面:
public void onClick(View v){
Intent intent = new Intent(MainActivity.this, NewSong.class);
startActivity(intent);
}
我从 4 个按钮开始,并且一切正常,然后决定使用 imageButtons 来代替美学。
我不确定“按钮”和“图像按钮”有什么区别,但您肯定不能互换它们。到目前为止,我最好的“谷歌搜索”还没有找到任何解决方案来理解如何修改我之前的“按钮”解决方案。
欢迎任何帮助,
埃德
这是 MainActivity.java 代码:
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//In this next line, note that 'btn' will never be used, it's
//grayed out in the "Button btn...", and in the (R.id.btn) it is
//shown in red font, indicating I probably need to declare a resource of some kind?
Button btn = (Button)findViewById(R.id.btn);
Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewSong.class);
startActivity(intent);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是 activity_main.xml:
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:clickable="true">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton"
android:background="@drawable/new_song"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageButton2"
android:background="@drawable/loadsong"
android:layout_alignTop="@+id/imageButton"
android:layout_toRightOf="@+id/imageButton"
android:layout_toEndOf="@+id/imageButton"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"/>
</RelativeLayout>
最佳答案
Android 中的ImageButton 不是Button 的子类,而是ImageView 的子类,因此不能将ImageButton 存储在Button 变量中。所以你需要使用 ImageButton 作为你的按钮对象类。 setOnClickListener 方法不是静态的,应该在您要使用的按钮对象上设置。此外,您尝试使用的 Button id 不在您的 xml 中(至少不在您发布的部分中)。所以你的代码应该是这样的:
ImageButton btn = (ImageButton)findViewById(R.id.imageButton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, NewSong.class);
startActivity(intent);
}
});
关于Android Studio imageButton 和切换到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28665323/