В предыдущей теме я спрашивал как реализовать сохранение данных при выходе из приложения. Следуя инструкции из урока:
http://startandroid.ru/ru/uroki/73-urok-33-hranenie-dannyh-preferences , я дополнил код своего тестового приложения и получилось следующее:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton1 = (Button) findViewById(R.id.button);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button5);
}
public void onCreate5(Bundle savedInstanceState) {
TextView skolkoTextView = (TextView) findViewById(R.id.etText);
skolkoTextView.setText("Сегодня я вспомнил " + ++mCount + " мишеней");
mButton1 = (Button) findViewById(R.id.button);
mButton2 = (Button) findViewById(R.id.button2);
mButton3 = (Button) findViewById(R.id.button3);
mButton4 = (Button) findViewById(R.id.button5);
LoadText();
}
@Override
protected void onDestroy() {
saveText();
super.onDestroy();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClick1(View view) {
TextView skolkoTextView = (TextView)findViewById(R.id.etText);
skolkoTextView.setText("Сегодня я вспомнил " + ++mCount + " мишеней");
}
public void onClick2(View view) {
TextView skolkoTextView = (TextView)findViewById(R.id.etText);
skolkoTextView.setText("Сегодня я вспомнил " + ++mCount + " мишеней");
}
public void onClick3(View view) {
TextView skolkoTextView = (TextView)findViewById(R.id.etText);
skolkoTextView.setText("Сегодня я вспомнил " + ++mCount + " мишеней");
}
public void onClick4(View view) {
TextView skolkoTextView = (TextView)findViewById(R.id.etText);
skolkoTextView.setText("Сегодня я вспомнил " + ++mCount + " мишеней");
}
}
Вот main.xml:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
36.
37.
38.
39.
40.
41.
42.
43.
44.
45.
46.
47.
48.
49.
50.
51.
52.
53.
54.
55.
56.
57.
58.
59.
60.
61.
62.
63.
64.
65.
66.
67.
68.
69.
70.
71.
72.
73.
74.
75.
76.
77.
78.
79.
80.
81.
82.
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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#ff4b0082">
<EditText
android:id="@+id/etText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="129dp"
android:layout_above="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<requestFocus>
</requestFocus>
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:id="@+id/button"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/buttonred"
android:onClick="onClick1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:id="@+id/button2"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/buttanorange"
android:onClick="onClick2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:id="@+id/button3"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/buttongreen"
android:onClick="onClick3"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/button5"
android:layout_toStartOf="@+id/button5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button4"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/perofotoshop"
android:layout_marginTop="57dp"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/etText"
android:layout_alignEnd="@+id/etText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="@+id/button5"
style="?android:attr/borderlessButtonStyle"
android:background="@drawable/buttonblue"
android:nestedScrollingEnabled="false"
android:onClick="onClick4"
android:layout_below="@+id/etText"
android:layout_alignRight="@+id/etText"
android:layout_alignEnd="@+id/etText" />
</RelativeLayout>
Но данные как не сохранялись, так и не сохраняются. Подскажите, пожалуйста в чем проблема?
И еще один небольшой вопросик: Вот например, я нажал на одну кнопку один раз, она изменила значение в счетчике на 1. Как при повторном нажатии на одну и ту же кнопку, сделать так, чтобы значение в счетчике уже не увеличивалось до перезапуска приложения.
Заранее благодарен.
Модератор: Просьба пользоваться тегами SRC JAVA и SPOLER.