使用两个按钮一个用于加法和一个减法和一个文本视图来显示数字,但是当我运行这个程序时,TwiteVIEW没有显示数字,比如在按下Add按钮值之后,当我使用JAVA的按钮和代码时看起来是1。
我也试过改名字,但没用
下面是Java代码
package com.example.junaid;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
Button mns, pls;
TextView tv;
int counter;
EditText ed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mns = (Button) findViewById(R.id.minuss);
pls = (Button) findViewById(R.id.pluss);
tv = (TextView) findViewById(R.id.textView2);
counter = 0;
mns.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter--;
tv.setText("Your Total is" + counter);
}
});
pls.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
tv.setText("Your Total is" + counter);
}
});
}
}
XML代码
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">
android:id="@+id/pluss"
android:layout_width="259dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
android:id="@+id/minuss"
android:layout_width="257dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="8dp"
android:text="Subtract"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/pluss"
tools:ignore="MissingConstraints" />
android:id="@+id/textView2"
android:layout_width="257dp"
android:layout_height="84dp"
android:layout_marginTop="8dp"
android:text="Your Total is"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />