gpt4 book ai didi

java - ImageButton 的图像未按预期更改

转载 作者:行者123 更新时间:2023-11-29 08:53:22 25 4
gpt4 key购买 nike

我的目标是从 ImageButton(ibChamp) 更改图像。

package com.example.custombuilds;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;z
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;

public class Champions extends Activity {

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

ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);

ibAnnie.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Check this for errors
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(view);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);

try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {

}

}
});
}
}

请注意,ImageButton ibChamp 来自 xml 布局 create_build_page,而不是 xml 冠军。此代码运行时不会崩溃,但来自 Imagebutton ibChamp 的图像不会改变,这正是我正在尝试做的。我很乐意提供任何其他附加信息。

最佳答案

您在 onClick 中夸大了“ibChamp”。这将创建一个新的 ImageButton。在使用父级的“addView”之前,您不会看到它。

您可以添加您的 XML,但您需要获取对 Activity 中现有按钮的引用才能更改它。或者添加新按钮并删除旧按钮...

换句话说,改变这个:

        LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.create_build_page, null);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);

为此:

        ImageButton img = (ImageButton)findViewById(R.id.ibChamp);

它可能会做你想做的事。

关于java - ImageButton 的图像未按预期更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21592015/

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