假設進口和東西Android Studio中計數器的TextView現場沒有更新
我試着去建立一個簡單的卡路里計數器,但其顯示 的卡路里數的麻煩。我也希望它如果用戶輸入500,400,300添加到先前的輸入.. 例如在文本字段 總應當是1200
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/CalorieAdd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="rav_singh.caloriecounter.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:text="Current Calories "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/caloriesNeeded"
android:textSize="30dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="69dp"
android:layout_below="@+id/calCount"
android:layout_alignParentEnd="true" />
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/calCount"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="40dp" />
<Button
android:text="Add "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/calorieAdd"
android:layout_marginTop="88dp"
android:layout_below="@+id/caloriesNeeded"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/calorieinput"
android:hint="Add Calories "
android:layout_marginTop="12dp"
android:layout_below="@+id/caloriesNeeded"
android:layout_alignStart="@+id/caloriesNeeded"
android:textAlignment="center" />
</RelativeLayout>
MainActivity。的java
public class MainActivity extends AppCompatActivity
{
EditText input;
TextView calTotal;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calTotal = (TextView) findViewById(R.id.calCount);
input = (EditText) findViewById(R.id.calorieinput);
Button calorieAdd = (Button) findViewById(R.id.calorieAdd);
calTotal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Float n1 = Float.valueOf(input.getText().toString());
calTotal.setText(Float.toString(n1));
}
});
}
}
如果用戶要關閉該應用程序,將數量仍然是什麼上次?或者將該應用重置爲 –
@RaviSingh:重置。 '這個數字仍然是最後一次'爲了這個需要使用[SharedPreferences](https://www.tutorialspoint.com/android/android_shared_preferences.htm) –
讓它工作謝謝...怎麼樣重置應用程序,每天你有什麼建議,我用它? –