单击按钮增加TextView [英] Increment TextView with button click
本文介绍了单击按钮增加TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
感谢您的帮助,但我仍在努力.我是这样做的:
thanks for the help but I'm still struggling. I did this:
xml中的按钮:
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:background="@android:color/white"
android:onClick="incrementScore"
android:text="@string/player" />
主代码(在末尾):
当我按下按钮时,应用崩溃.我应该将该代码放在哪里?
Context context;
TextView tv;
Button incrementer;
public void incrementScore(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_home);
tv=(TextView)findViewById(R.id.textView1);
incrementer=(Button)findViewById(R.id.button1);
context=this;
incrementer.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
String present_value_string = tv.getText().toString();
int present_value_int = Integer.parseInt(present_value_string);
present_value_int++;
tv.setText(String.valueOf(present_value_int));
}
});
}
推荐答案
在您的课程中创建:
int score = 0;
然后;
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// score operation like score += increment_value;
t1.setText(""+score);
}
});
如果您需要保存"退出应用程序的分数,则需要使用SharedPreferences来显示/更新值.
If you need to "save" the score ocne you quit the application, you need to use SharedPreferences to show/update the value.
这篇关于单击按钮增加TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文