[英]Android - Issue with Dialog Boxes/ clicking of buttons
我有一個游戲,用戶在畫布上四處移動,並且在這種情況下可以與某項互動,一只狗會觸發戰斗。
這會加載一個對話框,使用戶可以選擇戰斗還是逃跑。 用戶單擊“打架”,然后彈出另一個對話框,其中顯示一些文本(您的詳細信息和狗的詳細信息),並且在該文本下方有一個攻擊按鈕。
當您單擊“附加”按鈕時,會發生一些事情(不是不相關的事情),這實際上意味着用戶和狗的詳細信息會發生變化(例如DogHP降至18)。
所以我的問題是:
請查看下面的一些代碼片段,任何想法或指導將不勝感激:
代碼段:
void Fight()
{
final int DogHP = 25;
final int DogAtk = 15;
final int DogDef = 5;
final int DogSpeed = 20;
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getText(R.string.monster_dog_title));
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.fight, null);
builder.setView(view);
final AlertDialog DogBattle = builder.create();
View FightBtn = view.findViewById(R.id.dog_fight);
FightBtn.setOnClickListener(new OnClickListener()
{
boolean attacked = false;
@Override
public void onClick(View clicked)
{
if(clicked.getId() == R.id.dog_fight)
{
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getText(R.string.monster_fight));
builder.setMessage("Your Current Battle Details:\n" + "HP = " + UserHP + " Def = " + UserDef + " Atk = " + UserAtk + "\n\n" + "Your Enemy:\n" + "HP = " + DogHP + " Def = " + DogDef + " Atk = " + DogAtk);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.monsterbattle, null);
builder.setView(view);
final AlertDialog BattleRun = builder.create();
BattleRun.show();
View LeaveBattle = view.findViewById(R.id.Attack);
LeaveBattle.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View clicked)
{
if(clicked.getId() == R.id.Attack)
{
// SOME CODE GOES HERE
}
}
});
}
}
});
}
在此先感謝您的幫助。
我沒有嘗試過,但是我相信對話框中文本字段的ID是android.R.id.custom,因此您應該可以執行以下操作:
((TextView)dialog.findViewById(android.R.id.custom)).setText(newTextToShow)
不過,更好的方法可能是使用自定義對話框。
我決定完全廢棄上面的想法,因為在對話框中發生了太多事情,最終創建了一個新類,同時使用對象而不是變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.