所以我刚才问了一个关于如何通过点击一个按钮更改文本视图的问题,并且效果很好!
所以现在......如果我将1个textview作为计数器而另一个作为限制,我将如何实现它,当计数器达到限制时,textview1将返回0并且限制将增加。
我试过这个:
If (textViewCounter==textViewLimit);
textViewCounter.setText(0);
textViewLimit.setText(textViewLimit.getText().toString() + 100);
但这不起作用...... 我怎么能这样做?
编辑我的问题只是一个例子。我打算用它来操纵我的价值观。但这里有一切:问题代码位于btnBattle onClick的底部
package com.example.intent_buttontests;
import android.app.Activity;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class PlayScreen extends Activity {
private int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.play_screen);
String Name = getIntent().getStringExtra("strName");
TextView textview5 = (TextView) findViewById(R.id.textView5);
textview5.setText(Name);
textview5.setTextColor(Color.CYAN);
String Race = getIntent().getStringExtra("strRace");
TextView textViewStrRace = (TextView) findViewById(R.id.textViewStrRace);
textViewStrRace.setText(Race);
String Class = getIntent().getStringExtra("strClass");
TextView textViewStrClass = (TextView) findViewById(R.id.TextViewStrClass);
textViewStrClass.setText(Class);
String Alliance = getIntent().getStringExtra("strAlliance");
TextView textViewStrAlliance = (TextView) findViewById(R.id.textViewStrAlliance);
textViewStrAlliance.setText(Alliance);
Button btnInventory = (Button) findViewById(R.id.button1);
btnInventory.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Inventory.class);
startActivityForResult(intent, 0);
}
});
Button btnEquipment = (Button) findViewById(R.id.button2);
btnEquipment.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Equipment.class);
startActivityForResult(intent, 0);
}
});
Button btnStats = (Button) findViewById(R.id.button3);
btnStats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Statistics.class);
startActivityForResult(intent, 0);
}
});
Button btnShop = (Button) findViewById(R.id.Button01);
btnShop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Shop.class);
startActivityForResult(intent, 0);
}
});
Button btnCrafting = (Button) findViewById(R.id.button4);
btnCrafting.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), Crafting.class);
startActivityForResult(intent, 0);
}
});
Button btnClassTraining = (Button) findViewById(R.id.button5);
btnClassTraining.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(v.getContext(), ClassTraining.class);
startActivityForResult(intent, 0);
}
});
Button btnBattle = (Button) findViewById(R.id.btnBattle);
btnBattle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method
// Definitions
Spinner spinnerMonsters = (Spinner) findViewById(R.id.spinnerMonsters);
TextView textViewBattleResults = (TextView) findViewById(R.id.textViewBattleResults);
TextView textViewXPValue = (TextView) findViewById(R.id.textViewXPValue);
TextView textViewXpNextLevel = (TextView) findViewById(R.id.textViewXpNextLevel);
// When XP limit is reached, increases by 100 and sets back to 0
if (textViewXPValue.getText().toString().equals(textViewXpNextLevel.getText().toString())); {
textViewXPValue.setText("0");
int newLimit = Integer.parseInt(textViewXpNextLevel.getText().toString()) + 100;
textViewXpNextLevel.setText(newLimit+"");
}
if (spinnerMonsters.getItemAtPosition(0).toString().equals("(0) Training Dummy")); {
textViewBattleResults.setText("You have killed Training Dummy for 10 experience points!");
i = i+10;
textViewXPValue.setText(String.valueOf(i));
}
}
});
}
}
答案 0 :(得分:2)
而不是只比较两个textview的文本(字符串),
<强>更新强>
If (textViewCounter.getText().toString().equals(textViewLimit.getText().toString()));
{
textViewCounter.setText(0);
int newLimit = Integer.parseInt(textViewLimit.getText().toString()) + 100;
textViewLimit.setText(newLimit+"");
}
答案 1 :(得分:1)
当然不行,你无法直接比较textView的内容。你应该做的是创建一个String来保存文本视图的内容。
String stextViewCounter = textViewCounter.getText().toString();
String stextViewLimit = textViewLimit.getText().toString();
if(stextViewCounter.equal(stextViewLimit)){
textViewCounter.setText(0);
textViewLimit.setText(stextViewLimit + 100);
}
除非你使用intergers然后你必须使用parse int
解析它答案 2 :(得分:0)
If (textViewCounter.getText().toString().equals(textViewLimit.getText().toString()));
{
textViewCounter.setText("");
int n = Integer.parseInt(textViewLimit.getText().toString()) + 100;
textViewLimit.setText("" + n);
}
答案 3 :(得分:-1)
尝试使用此
If (textViewCounter.getText().toString().equals(textViewLimit.getText().toString()))
textViewCounter.setText(0);
textViewLimit.setText(textViewLimit.getText().toString() + 100);
这可能会对你有所帮助