Java 关于@String上的加法
我在字符串xml上设置了一个字符串Java 关于@String上的加法,java,android,Java,Android,我在字符串xml上设置了一个字符串 <string name="plus">1</string> 1 我有一个这样的文本视图 <TextView android:text="@string/plus" android:layout_width="100dp" android:layout_height="50dp" android:textSize="30sp" androi
<string name="plus">1</string>
1
我有一个这样的文本视图
<TextView
android:text="@string/plus"
android:layout_width="100dp"
android:layout_height="50dp"
android:textSize="30sp"
android:textColor="#990000"
android:gravity="center"
android:id="@+id/pluson" />
我有一个这样的按钮
<Button
android:text="Plus One"
android:textSize="14sp"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:layout_height="30dp"
android:id="@+id/plus"
android:background="@drawable/button"
android:layout_width="280dp" />
我想当有人点击这个“加一”按钮;我的文本视图每次都显示下一个数字。比如1-2-3-4-5等等
虽然我在youtube上尝试了一些代码,但在互联网上发现它只是叠加在“1”上或显示在“213412465等”上,或者只是说一些信息,如u不能上瘾字符串等
非常感谢…您必须从文本视图中获取文本,然后必须将此文本转换为int值并添加一个,分数转换为字符串并在文本视图中设置
Button myButton = (Button) findViewById(R.id.plus);
TextView myTexView = (TextView)findViewById(R.id.pluson);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int value = Integer.valueOf(myTextView.getText().toString());
myTextView.setText(String.valueOf(value++));
}
});
您必须从textView中获取文本,接下来必须将此文本转换为int值并添加一个,将分数转换为字符串并在textView中设置
Button myButton = (Button) findViewById(R.id.plus);
TextView myTexView = (TextView)findViewById(R.id.pluson);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int value = Integer.valueOf(myTextView.getText().toString());
myTextView.setText(String.valueOf(value++));
}
});
My.xml文件:
My.xml文件:
显示您的java代码显示您的java代码
Button btn_increase = (Button) findViewById(R.id.plus);
TextView textView = (TextView) findViewById(R.id.pluson);
btn_increase.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int num = Integer.parseInt(textView.getText().toString());
textView.setText(String.valueOf(num++));
}
});