2015-10-18 35 views
0

這可能是一個令人困惑的問題,但我會盡量使其儘可能簡單。Android主號碼

好的,我正在做一個遊戲,在遊戲中,有一個主號碼叫金錢。我想要的是,如果這筆錢是一個特定的價值,我希望他們能夠按下一個按鈕,這將添加一筆特定數額的錢到原來的錢,我想新的主數字是總和二。我知道一個變量不能被初始化兩次,所以我想知道如何去做這件事。我只是一個初學者,所以任何幫助/提示都表示讚賞。

if money < cost 
enable button 
else disable button 
when pressed, money + cost 
output master money 

回答

0

我創建了一個簡單的應用程序來幫助您開始。我爲貨幣主號碼設置了一個特定的值。您將需要用您的邏輯來替換該分配。

這裏是佈局文件:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/tvMoney" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Claim Money" 
     android:id="@+id/button" /> 

</LinearLayout> 

這裏的活動:

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

import java.text.NumberFormat; 

public class MainActivity extends AppCompatActivity { 

    Double money = 14.00; 
    Double cost = 15.00; 
    NumberFormat format = NumberFormat.getCurrencyInstance(); 

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

     Button button = (Button) findViewById(R.id.button); 
     ((TextView) findViewById(R.id.tvMoney)).setText(format.format(money)); 

     button.setEnabled(false); 
     if (money < cost) { 
      button.setEnabled(true); 
     } 

     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View view) { 
       money += cost; 
       ((TextView) findViewById(R.id.tvMoney)).setText(format.format(money)); 
      } 
     }); 
    } 
} 
+0

哇,非常感謝。不過,我只有一個問題。說出是否還有一個按鈕,並且我希望該按鈕執行與此相同的任務,但該按鈕使用更新後的金錢價值。當我第二次使用可變貨幣時,它將使用14,而不是更新後的29,對嗎?我希望它使用29,這也是我之前的主要問題。 – user5145575

+0

查看我上面的更改。我添加了'money + = cost'這一行。這將更新金錢的價值。我還刪除了以下行中的「成本」的添加。合理? –

+0

謝謝,這正是我一直在尋找的。 – user5145575