gpt4 book ai didi

android - 检查 Android 中是否存在按钮

转载 作者:太空宇宙 更新时间:2023-11-03 13:27:55 26 4
gpt4 key购买 nike

再次需要一些建议。

我正在处理的这个模块已完成 99%,尽管我还停留在最后一个障碍上。我在运行时在屏幕上动态发布一个按钮。按下此按钮会将其带到新的 Activity View 。我得到了完美的工作。

但是,我有另一个随机更改 View 的按钮,因此如果有意义的话,它实际上需要自行重置。发生的事情是每次单击按钮(动态按钮)时,它都会将另一个按钮添加到堆栈中。实际上,每次我点击运行屏幕时,我都有按钮。我的逻辑是错误的还是有办法检查并防止按钮每次都显示?即只有一次......下面是代码。

public void ButtonOnClick(View v) {


Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);

Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;

switch (randomListIndex) {
case 0:

//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);

button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");

Button myButton = new Button(this);
myButton.setText("Press Me");


LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);

final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View arg0) {

String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);

}
});

break;
}
}

attached is a screen shot to what I'm seeing

最佳答案

编辑:这个怎么样?添加一个新的类字段成员(类变量,而不是方法的局部变量),指示按钮是否实际添加到布局中。

private boolean buttonShown = false; /* Here changed */

public void ButtonOnClick(View v) {


Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);

Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;

switch (randomListIndex) {
case 0:

//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);

button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");

if (buttonShown == false) { /* Here changed */

Button myButton = new Button(this);
myButton.setText("Press Me");

LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);

final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View arg0) {

String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);

}
});

buttonShown = true; /* Here changed */
} /* Here changed */

break;
}
}

再次编辑:我使用类字段成员 pressMeButton 而不是局部变量 myButton。

private Button pressMeButton;

public void ButtonOnClick(View v) {


Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);

Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;

switch (randomListIndex) {
case 0:

//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);

button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");

if (pressMeButton == null) {
pressMeButton = new Button(this);
pressMeButton.setText("Press Me");

LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(pressMeButton);
}

/* If the pressMeButton is already in the layout, all you need to do is just changing the onClickListener. */
pressMeButton.setOnClickListener(new View.OnClickListener()
{

@Override
public void onClick(View arg0) {

String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);

}
});

break;
}
}

关于android - 检查 Android 中是否存在按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16847799/

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