gpt4 book ai didi

java - 如何获取字符串数组中的下一个按钮

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:58:39 30 4
gpt4 key购买 nike

在一个类中,我得到了一个包含事实的字符串,数组中是这样的:

public class FactBook {
public String[] mFacts = {"a", "b", "c"};
....

现在我得到了一个这样的随机生成器:

public String getFact() {
String fact = "";

Random randomGenerator = new Random();

int randomNumber = randomGenerator.nextInt(mFacts.length);

fact = mFacts[randomNumber];

return fact;
}

我在activity类中的randomButton是这样的:

final TextView factLabel = (TextView) findViewById(R.id.factTextView);
final Button showFactButton = (Button) findViewById(R.id.showFactButton);
final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);
final MediaPlayer mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.button);

View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
mMediaPlayer.start();

String fact = mFactBook.getFact();
//Update the Label with our dynamic fact
factLabel.setText(fact);

int color = mColorWheel.getColor();
relativeLayout.setBackgroundColor(color);
showFactButton.setTextColor(color);
}
};
showFactButton.setOnClickListener(listener);

现在我想创建一个逐步遍历数组的按钮(如 nextButtonpreviousButton)。但我卡住了。有人有什么想法吗?

最佳答案

创建接受字符串数组并对其进行操作的类,实现非常基础,视为 poc ;)

public class Facts
{
private String[] mFacts;
private int current = 0;
public Facts( String[] mFacts )
{
this.mFacts = mFacts;
}

public boolean hasPrevious()
{
return current >= 0;
}

public boolean hasNext()
{
return current < mFacts.length;
}

public String next()
{
if( !hasNext() )
{
return null;
}
String v = mFacts[ current ];
current++;
return v;
}

public String previous()
{
if( !hasPrevious() )
{
return null;
}
String v = mFacts[ current ];
current--;
return v;
}

public String random()
{
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(mFacts.length);
return mFacts[randomNumber];
}
}

更新代码中的某处 Facts facts = new Facts( String[] array );虽然我不熟悉 android,但我在你的源代码中看到按钮是在另一个地方创建的(可能通过一些 gui 工具)。每次单击按钮时,它将遍历数组直到到达末尾。要动态创建按钮(从文档中读取),您可以使用它。

public class ButtonCreator
{
private Facts facts;
public ButtonCreator( Facts facts )
{
this.facts = facts;
}

public boolean canCreateNextButton() { return facts.hasNext(); }
public boolean canCreatePreviousButton() { return facts.hasPrevious(); }
public Button createNextButton( Context context )
{
Button button = new Button( context );
button.setText( facts.next() );
return button;
}
public Button createPreviousButton( Context context )
{
Button button = new Button( context );
button.setText( facts.previous() );
return button;
}
}

然后在创建后像粘贴代码一样设置适当的监听器

然后在表单初始化期间

String[] arr = {"AAA","BBBB","CCC"};
Facts f = new Facts( array );
ButtonCreator creator = new ButtonCreator( f );

while( creator.canCreateNextButton() )
{
Button button = creator.nextButton( //view context here// );
button.setListener( //listener here// );
}

关于java - 如何获取字符串数组中的下一个按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31234687/

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