我正在开发一款游戏,我需要从设定值中扣除。设定值是$ 10,000,我有五个editTexts,用户可以输入数量,然后这些数量乘以不同的$值,例如,2 * $ 400,1 * $ 600,3 * $ 100 ...然后我想要那些总数在输入时从设定值中扣除,以便玩家可以看到剩余10,000个中的多少。
我已经能够使用onTextChangedListener使用单个editText来完成此任务。
我没有测试下面的代码,但在我的应用程序中它是这样的。
mEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start,
int before, int count) {
BigDecimal totalValue = new BigDecimal("10000");
mTotalValueTextView.setText(totalValue);
if (s.length() > 0) {
BigDecimal quantityEntered = new BigDecimal(s.toString());
BigDecimal amountPurchased = quantityEntered.multiply(new BigDecimal("400"));
BigDecimal newTotalValue = totalValue.subtract(amountPurchased);
mTotalValueTextView.setText(newTotalValue + "");
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
在这种情况下,如果用户输入数量1,则总值将立即下降400,数量2将减少800,依此类推。问题是我不知道如何让另一个editText和onTextChangedListener从相同的值中减去。也许我应该采取另一种方法?我写这篇文章已经很晚了,请告诉我原代码是否有问题,我会解决。
这是我最初的活动。我试图简化上面的例子,但也许这会有所帮助。
package oneonanyone.com.fantasybasketball_1onany1;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DecimalFormat;
import oneonanyone.com.fantasybasketball_1onany1.DataModel.Player;
public class MatchupActivity extends Activity {
private Player[] mPlayers;
private TextView mPlayerName;
private TextView mPlayerValue;
private TextView mPlayerRemainingValue;
private TextView mPoints;
private TextView mRebounds;
private TextView mAssists;
private TextView mSteals;
private TextView mBlocks;
private TextView mOpponentName;
private TextView mOpponentValue;
private TextView mOpponentRemainingValue;
private TextView mOpponentPoints;
private TextView mOpponentRebounds;
private TextView mOpponentAssists;
private TextView mOpponentSteals;
private TextView mOpponentBlocks;
private EditText mPtEditText;
private EditText mRebEditText;
private EditText mAssEditText;
private EditText mStlEditText;
private EditText mBlkEditText;
private Button mAcceptMatchButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_matchup);
mPlayerName = (TextView) findViewById(R.id.playerNameTextView);
mPlayerValue = (TextView) findViewById(R.id.playerValueTextView);
mPlayerRemainingValue = (TextView) findViewById(R.id.remainingCapTextView);
mPoints = (TextView) findViewById(R.id.pointsTextView);
mRebounds = (TextView) findViewById(R.id.reboundsTextView);
mAssists = (TextView) findViewById(R.id.assistsTextView);
mSteals = (TextView) findViewById(R.id.stealsTextView);
mBlocks = (TextView) findViewById(R.id.blocksTextView);
mOpponentName = (TextView) findViewById(R.id.opponentTextView);
mOpponentValue = (TextView) findViewById(R.id.opponentValue);
mOpponentRemainingValue = (TextView) findViewById(R.id.opponentCap);
mOpponentPoints = (TextView) findViewById(R.id.opponentPoints);
mOpponentRebounds = (TextView) findViewById(R.id.opponentReb);
mOpponentAssists = (TextView) findViewById(R.id.opponentAssists);
mOpponentSteals = (TextView) findViewById(R.id.opponentSteals);
mOpponentBlocks = (TextView) findViewById(R.id.opponentBlocks);
mPtEditText = (EditText) findViewById(R.id.ptEditText);
mRebEditText = (EditText) findViewById(R.id.rebEditText);
mAssEditText = (EditText) findViewById(R.id.assEditText);
mStlEditText = (EditText) findViewById(R.id.stlEditText);
mBlkEditText = (EditText) findViewById(R.id.blkEditText);
mAcceptMatchButton = (Button) findViewById(R.id.acceptMatchButton);
final Bundle extras = getIntent().getExtras();
mPlayerRemainingValue.setText("Funds Remaining: $" + extras.getString("PLAYER_CAP"));
mPlayerName.setText(extras.getString("PLAYER_NAME"));
mPlayerValue.setText("Price: $" + extras.getString("PLAYER_VALUE"));
mPoints.setText("Points " + extras.getString("PLAYER_POINTS"));
mRebounds.setText("Rebounds " + extras.getString("PLAYER_REBOUNDS"));
mAssists.setText("Assists " + extras.getString("PLAYER_ASSISTS"));
mSteals.setText("Steals " + extras.getString("PLAYER_STEALS"));
mBlocks.setText("Blocks " + extras.getString("PLAYER_BLOCKS"));
mOpponentName.setText(extras.getString("OPPONENT_NAME"));
mOpponentValue.setText("Price: $" + extras.getString("OPPONENT_VALUE"));
mOpponentRemainingValue.setText("Funds Remaining: $" + extras.getString("OPPONENT_CAP"));
mOpponentPoints.setText("Points " + extras.getString("OPPONENT_POINTS"));
mOpponentRebounds.setText("Rebounds " + extras.getString("OPPONENT_REBOUNDS"));
mOpponentAssists.setText("Assists " + extras.getString("OPPONENT_ASSISTS"));
mOpponentSteals.setText("Steals " + extras.getString("OPPONENT_STEALS"));
mOpponentBlocks.setText("Blocks " + extras.getString("OPPONENT_BLOCKS"));
mPtEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
BigDecimal dynamicCapValue = new BigDecimal(extras.getString("PLAYER_CAP"));
mPlayerRemainingValue.setText("Funds Remaining: $" + dynamicCapValue);
if (s.length() > 0) {
BigDecimal input = new BigDecimal(s.toString());
BigDecimal ptsPurchased = input.multiply(new BigDecimal("400"));
BigDecimal initialCap = new BigDecimal(extras.getString("PLAYER_CAP"));
dynamicCapValue = initialCap.subtract(ptsPurchased);
mPlayerRemainingValue.setText("Funds Remaining: $" + dynamicCapValue);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
mRebEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mPlayerRemainingValue.setText("Funds Remaining: $" + extras.getString("PLAYER_CAP"));
if (s.length() > 0) {
BigDecimal input = new BigDecimal(s.toString());
BigDecimal rebPurchased = input.multiply(new BigDecimal("800"));
BigDecimal initialCap = new BigDecimal(extras.getString("PLAYER_CAP"));
BigDecimal remainingValue = initialCap.subtract(rebPurchased);
mPlayerRemainingValue.setText("Funds Remaining: $" + remainingValue);
}
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public BigInteger getDollarAmount(){
String ptQuantity = mPtEditText.getText().toString();
BigInteger ptQuantity1 = new BigInteger(ptQuantity);
BigInteger ptDollarAmount = new BigInteger("400");
BigInteger ptTotalDollarAmount = ptQuantity1.multiply(ptDollarAmount);
return ptTotalDollarAmount;
}
}
答案 0 :(得分:0)
萨拉姆!
此方法将您的需求作为double返回:
public double sumInputs(double totalValue) {
EditText[] inputs = {mPtEditText, mRebEditText,
mAssEditText, mStlEditText, mBlkEditText};
double sum = 0.0;
for (int i = 0; i < inputs.length; i++)
sum = sum + Double.parseDouble(inputs[i].getResources().toString());
return totalValue - sum;
}
因此,您可以将此TextWatcher
设置为EditText
:
TextWatcher watcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
sumInputs(400);
}
};
注意: 400是totalValue
告诉我你是否需要更多帮助...