android模拟器按钮在哪,模拟器不显示按钮上的图像点击Android

博主在尝试在Android应用中实现点击按钮显示图像的功能,但在布局XML文件和Java代码中设置了相应逻辑后,模拟器并未按预期显示图像。问题可能在于缺少ImageView组件,以及对按钮点击事件的处理。讨论中提出了可能的布局损坏和缺少相关控件的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

请耐心等待,我是新来的android编程。我试图在android中点击按钮显示图像,但不幸的是我的模拟器没有显示所需的输出。这是我的layout.xml文件模拟器不显示按钮上的图像点击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"

tools:context=".MainActivity" >

android:id="@+id/button1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/textView1"

android:layout_below="@+id/textView1"

android:layout_marginTop="14dp"

android:text="Button" />

main_activity.java

package com.example.app1;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.widget.Button;

import android.widget.ImageView;

import android.view.View;

import android.view.View.OnClickListener;

public class MainActivity extends Activity {

Button button;

ImageView image;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}

@Override

public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.

getMenuInflater().inflate(R.menu.main, menu);

return true;

}

public void addListenerOnButton() {

button = (Button) findViewById(R.id.button1);

button.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View arg0) {

image.setImageResource(R.drawable.optimus);

}

});

}

}

我已经列入/ RES /绘制-MDPI的图像文件。当我尝试在模拟器上运行时,它不会在按钮单击时显示图像。代码有问题吗?在使用它之前

Button theButton = (Button)findViewById(R.id.button1);

theButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

theButton.setBackgroundResource(R.drawable.optimus);

}

2014-06-08

Darpit

+0

你甚至没有在你的Java文件中的按钮,更不用说一个点击监听器或图像 –

+0

我没有得到你。请你可以编辑我的代码并指出我的错误。 Thx提前预测。 –

+1

您的布局中没有** ImageView **。你如何假装把图像放在一个不存在的物体上?并且您也没有名为** textView1 **的控件**'android:layout_alignLeft =“@ + id/textView1”'或者我应该假定您**已经损坏了您的布局?你能完全展示**吗? –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值