现在我的按钮计数器工作了,点击时它会增加,但我现在的问题是它不会保存,所以当它重新打开时它会重新开始......我把我的代码放在下面,但任何帮助添加保存功能都会不胜感激!
package com.example.counter;
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView;
public class MainActivity extends Activity {
// Private member field to keep track of the count
private int mCount = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final TextView countTextView = (TextView) findViewById(R.id.TextViewCount);
final Button countButton = (Button) findViewById(R.id.ButtonCount);
countButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mCount++;
countTextView.setText("Count: " + mCount);
}
});
}
}
xml布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/TextViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/ButtonCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/ButtonCount"
android:layout_alignRight="@+id/ButtonCount"
android:layout_marginBottom="61dp"
android:text="Count" />
</RelativeLayout>