Gun's Blog

Android应用开发技术

Word count: 3.3kReading time: 18 min
2019/08/29 Share

1.首先用视图模式新建一个项目`Caculator’,下面是新建好的android目录结构:
目录结构

2.接下来是编写布局文件dispaly.xml:
整体采用表格布局<TableLayout></TableLayout>6个<TableRow></TableRow>,并为所有的按钮增加唯一的id,以便设计监听事件时控制相应的控件,设计好的界面如图所示:
界面
dispaly.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="0">
<TableRow>
<EditText
android:layout_width="match_parent"
android:layout_height="100sp"
android:hint="请输入要计算的式子:"
android:text=""
android:id="@+id/input"/>
</TableRow>
<TableRow>
<Button
android:text="1"
android:layout_column="0"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="80sp"
android:layout_margin="2dp"
android:id="@+id/bt1"/>
<Button
android:text="2"
android:layout_column="1"
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:id="@+id/bt2" />
<Button
android:text="3"
android:layout_column="2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="80sp"
android:layout_margin="2dp"
android:id="@+id/bt3"/>
<Button
android:layout_width="0dp"
android:layout_column="3"
android:layout_height="80sp"
android:layout_weight="1"
android:text="x"
android:background="#ffffff"
android:layout_margin="2dp"
android:id="@+id/bt_mul"/>
</TableRow>
<TableRow>
<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="4"
android:id="@+id/bt4"/>

<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="5"
android:id="@+id/bt5"/>

<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="6"
android:id="@+id/bt6"/>

<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:background="#ffffff"
android:text="+"
android:id="@+id/bt_add"/>
</TableRow>

<TableRow>
<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="7"
android:id="@+id/bt7"/>


<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="8"
android:id="@+id/bt8"/>
<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:text="9"
android:id="@+id/bt9"/>
<Button
android:layout_width="0dp"
android:layout_height="80sp"
android:layout_weight="1"
android:layout_margin="2dp"
android:background="#ffffff"
android:text="-"
android:id="@+id/bt_sub" />
</TableRow>

<TableRow>
<Button
android:text="="
android:layout_height="80sp"
android:layout_width="0dp"
android:layout_weight="2"
android:layout_margin="2dp"
android:id="@+id/bt_equ" />
<Button
android:text="0"
android:layout_height="80sp"
android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/bt0" />
<Button
android:text="/"
android:layout_margin="2dp"
android:layout_height="80sp"
android:background="#ffffff"

android:layout_width="0dp"
android:layout_weight="1"
android:id="@+id/bt_div" />
</TableRow>
<TableRow>
<Button
android:text="清空"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="80sp"
android:background="#ffffff"
android:layout_margin="2dp"
android:id="@+id/bt_clc" />

<Button
android:text="Del"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#ffffff"
android:layout_height="80sp"
android:layout_margin="2dp"
android:id="@+id/bt_del" />
</TableRow>
</TableLayout>

3.接下来是核心代码的编写:
思路:对所有的按钮进行监听,当点击的是0-9还有运算符时,在编辑框内显示按钮的文本内容,当点击按钮=时在编辑框内显示计算的结果.核心代码及注释如下所示:

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package top.oriheart.caculator;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


//使用OnClickListener接口,实现onClick方法.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button bt0, bt1, bt2, bt3, bt4, bt5, bt6, bt7, bt8, bt9, bt_add, bt_sub, bt_mul, bt_div, bt_clc, bt_del, bt_equ;
EditText input;
boolean clc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display);
bt0 = findViewById(R.id.bt0);
bt1 = findViewById(R.id.bt1);
bt2 = findViewById(R.id.bt2);
bt3 = findViewById(R.id.bt3);
bt4 = findViewById(R.id.bt4);
bt5 = findViewById(R.id.bt5);
bt6 = findViewById(R.id.bt6);
bt7 = findViewById(R.id.bt7);
bt8 = findViewById(R.id.bt8);
bt9 = findViewById(R.id.bt9);
bt_add = findViewById(R.id.bt_add);
bt_sub = findViewById(R.id.bt_sub);
bt_mul = findViewById(R.id.bt_mul);
bt_div = findViewById(R.id.bt_div);
bt_clc = findViewById(R.id.bt_clc);
bt_del = findViewById(R.id.bt_del);
bt_equ = findViewById(R.id.bt_equ);
input = findViewById(R.id.input);

bt0.setOnClickListener(this);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
bt3.setOnClickListener(this);
bt4.setOnClickListener(this);
bt5.setOnClickListener(this);
bt6.setOnClickListener(this);
bt7.setOnClickListener(this);
bt8.setOnClickListener(this);
bt9.setOnClickListener(this);
bt_add.setOnClickListener(this);
bt_sub.setOnClickListener(this);
bt_mul.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_equ.setOnClickListener(this);
bt_clc.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_div.setOnClickListener(this);

}

public void onClick(View view) {
String str = input.getText().toString(); //当不加toString时报错
switch (view.getId()) {
case R.id.bt0:
case R.id.bt1:
case R.id.bt2:
case R.id.bt3:
case R.id.bt4:
case R.id.bt5:
case R.id.bt6:
case R.id.bt7:
case R.id.bt8:
case R.id.bt9:
if (clc) {
clc = false;
str = "";
input.setText("");
}
input.setText(str + ((Button) view).getText());//强制类型转换
break;
case R.id.bt_add:
case R.id.bt_sub:
case R.id.bt_mul:
case R.id.bt_div:
if (clc) {
clc = false;
str = "";
input.setText("");
}
input.setText(str + " " + ((Button) view).getText() + " ");
break;
case R.id.bt_del:
if (clc) {
clc = false;
str = "";
input.setText("");
} else if (str != null && !str.equals("")) {
input.setText(str.substring(0, str.length() - 1));
}
break;
case R.id.bt_clc:
clc = false;
str = "";
input.setText("");
case R.id.bt_equ:
result();
break;
}
}

private void result() {
String expe = input.getText().toString();
if (expe == null || expe.equals("")) {
return;
}
if (!expe.contains(" ")) {
return;
}
if (clc) {
clc = false;
return;
}
clc = true;
double result = 0;
String s1 = expe.substring(0, expe.indexOf(" "));
String op = expe.substring(expe.indexOf(" ") + 1, expe.indexOf(" ") + 2);
String s2 = expe.substring(expe.indexOf(" ") + 3);
if (!s1.equals(" ") && !s2.equals(" ")) {
double d1 = Double.parseDouble(s1);
double d2 = Double.parseDouble(s2);
if (op.equals("+")) {
result = d1 + d2;
} else if (op.equals("-")) {
result = d1 - d2;
} else if (op.equals("x")) {
result = d1 * d2;
} else if (op.equals("/")) {
if (d2 == 0) {
input.setText("0不能作为除数!");
return;
} else {
result = d1 / d2;
}
} else if (!s1.equals("") && s2.equals("")) {
input.setText(expe);
} else if (s1.equals("") && !s2.equals("")) {
if (op.equals("+")) {
result = 0 + d2;
} else if (op.equals("-")) {
result = 0 - d2;
} else if (op.equals("x")) result = 0;
else if (op.equals("/")) {
result = 0;
}
}
}
input.setText(result+"");
}
}

4.运行代码后,结果显示:
计算器

5.打包成APK:
第一步

第二步

第三步
第一次打包,选择Create new即可.

第四步

  • key store path:需要以jks结尾
  • password :设置密码
  • Alias:自定义一个别名
  • password:密码,可以以第一个相同
  • certificate:至少填写一个

第五步
在相应文件夹下便可以找到打包的.apk android端应用程序.

6.增加一个导航页面并返回导航页面时保存计算器编辑框里的数据.界面如图所示:
导航页面

(1)导航界面设计:
思路:采用一个垂直的线性布局里添加一个水平线性布局:
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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<EditText
android:layout_width="match_parent"
android:layout_height="80sp"
android:id="@+id/et"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:orientation="horizontal"
android:layout_margin="8dp">
<Button
android:layout_width="0dp"
android:layout_height="160dp"
android:layout_weight="1"
android:layout_margin="8dp"
android:background="@color/colorAccent"
android:text="计算器"
android:id="@+id/cal"/>
<Button
android:layout_width="0dp"
android:layout_height="160dp"
android:layout_weight="1"
android:layout_margin="8dp"
android:background="@color/colorPrimary"
android:text="更多"/>

</LinearLayout>
</LinearLayout>

(2)利用intent回传数据:
思路:
在导航页面创建一个Intent实列,用于启动和接受回传计算器页面的数据,在计算器页面也创建一个Intent实列,并向导航页面回传数据.
page.java代码:
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
package top.oriheart.caculator;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class page extends AppCompatActivity implements View.OnClickListener {
EditText store;
Button bt_cal;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
store=findViewById(R.id.et);
bt_cal=findViewById(R.id.cal);
bt_cal.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.cal:
Intent intent=new Intent(this,MainActivity.class);
startActivityForResult(intent,1);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
switch (requestCode){
case 1:
if(resultCode==2){
String resultData=data.getStringExtra("exp") ;
System.out.println(resultData);
store.setText((resultData));
Log.d("page ",resultData);
}
break;
}
}
}

(3)计算器页面添加的代码:
1
2
3
4
5
6
7
switch (view.getId()) {
case R.id.store:
Intent intent=new Intent(this,page.class);
intent.putExtra("exp",input.getText().toString());
setResult(2,intent);
input.setText("");
finish();

(4),最终效果:当在导航页面点击计算器时跳转到计算器页面,当在计算器页面点击返回并保存时返回到导航页面并在编辑框内显示保存的数据。

显示结果

一些对话框、监听器的练习:

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
83
84
85
86
87
88
89
90
91
92
package top.oriheart.ordering;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.practice);

Button bt1 = findViewById(R.id.bt1);
RadioGroup radioGroup=findViewById(R.id.radioGroup);
final TextView textView=findViewById(R.id.textview);
// bt1.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Toast.makeText(MainActivity.this, "我被点击2",Toast.LENGTH_LONG).show();
// }
// });
// bt1.setOnClickListener(myclick);

//
// radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
// @Override
// public void onCheckedChanged(RadioGroup group, int checkedId) {
// if (checkedId == R.id.rbtn) {
// textView.setText("您的性别是:男");
// } else {
// textView.setText("您的性别是:女");
// }
// }
// });

// AlertDialog dialog;
// dialog=new AlertDialog.Builder(this)
// .setTitle("Dialog对话框")
// .setMessage("确定是否退出")
// .setIcon(R.mipmap.ic_launcher)
// .setPositiveButton("确定",null)
// .setNegativeButton("取消",null)
// .create();
// dialog.show();

// AlertDialog dialog;
// dialog=new AlertDialog.Builder(this)
// .setTitle("请选择性别:")
// .setIcon(R.mipmap.ic_launcher)
// .setSingleChoiceItems(new String[]{"男", "女"}, 0,
// new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// System.out.println(i);
// Toast.makeText(MainActivity.this,"哈哈",Toast.LENGTH_LONG);
// }
// })
// .setPositiveButton("确定",null)
// .create();
// dialog.show();


// final ProgressDialog prodialog;
// prodialog = new ProgressDialog(MainActivity.this);
// prodialog.setTitle("进度条对话框");
// prodialog.setIcon(R.mipmap.ic_launcher);
// prodialog.setMessage("正在下载请等候...");
// prodialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// prodialog.show();

}
//public void click(View view){
//Toast.makeText(this, "我被点击",Toast.LENGTH_LONG).show();
//}

// View.OnClickListener myclick=new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Toast.makeText(MainActivity.this, "我被点击4",Toast.LENGTH_LONG).show();
// }
// };
}

结课考试复习

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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
package com.example.layouttest;

import androidx.annotation.ContentView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.strictmode.SqliteObjectLeakedViolation;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.ArrayList;

import static android.view.View.inflate;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button login, register, readData,Delete,Update;
EditText editText1;
EditText editText2;
ImageView imageView;
private DBHelper dbHelper;
int listItemCount;
// String []disUserID=new String[];
// String []disUserPasswd;
ArrayList disUserID=new ArrayList();
ArrayList disUserPasswd=new ArrayList();
ListView listView;
String[] userID;
String[] passwd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.review);
login = findViewById(R.id.login);
register = findViewById(R.id.register);
// login.setOnClickListener(myListener);//独立类的方式实现
// register.setOnClickListener(myListener);
login.setOnClickListener(this);
register.setOnClickListener(this);

editText1 = findViewById(R.id.user_ID);
editText2 = findViewById(R.id.user_password);

imageView = findViewById(R.id.image);
imageView.setOnClickListener(this);
readData = findViewById(R.id.readdata);
readData.setOnClickListener(this);
listView=findViewById(R.id.userInfo);

Delete=findViewById(R.id.delete);
Update=findViewById(R.id.update);

Delete.setOnClickListener(this);
Update.setOnClickListener(this);

dbHelper=new DBHelper(this,"user.db",null,1);
// login.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view){
// Toast.makeText(MainActivity.this, "login button is clicked(匿名类)", Toast.LENGTH_LONG).show();
// }
// });
// register.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Toast.makeText(MainActivity.this, "register button is clicked(匿名类)", Toast.LENGTH_LONG).show();
//
// }
// });

}

// public void registerButton(View view) {
// Toast.makeText(this, "register button is clicked", Toast.LENGTH_LONG).show();
// }

// View.OnClickListener myListener = new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// switch (view.getId()) {
// case R.id.register:
// Toast.makeText(MainActivity.this, "register button is clicked", Toast.LENGTH_LONG).show();
// break;
// case R.id.login:
// Toast.makeText(MainActivity.this, "login button is clicked", Toast.LENGTH_LONG).show();
// break;
// }
// }
//
// };
//
// @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.register:
//// {
////// Toast.makeText(MainActivity.this, "register button is clicked", Toast.LENGTH_LONG).show();
//// AlertDialog dialog;
//// dialog=new AlertDialog.Builder(this)
//// .setTitle("提示")
//// .setIcon(R.drawable.ic_launcher_background)
//// .setMessage("register button is clicked")
//// .setPositiveButton("确定", new DialogInterface.OnClickListener() {
//// @Override
//// public void onClick(DialogInterface dialogInterface, int i) {
//// Toast.makeText(MainActivity.this,"positive button is clicked",Toast.LENGTH_LONG).show();
////
//// }
//// })
//// .setNegativeButton("取消",null)
//// .create();
//// dialog.show();
////
//// }
// Intent intent = new Intent(this, DataDisplay.class);
// Bundle bundle = new Bundle();
// bundle.putString("userID", editText1.getText().toString());
// bundle.putString("userPassword", editText2.getText().toString());
// intent.putExtras(bundle);
// startActivity(intent);
// break;
// String userID=editText1.getText().toString();
// String userPassword=editText2.getText().toString();
// String filename="data.txt";
// FileOutputStream fos;
// try{
// fos=openFileOutput(filename,MODE_PRIVATE);
// fos.write((userID+"##"+userPassword).getBytes());
// fos.close();
// }catch (Exception e){
// e.printStackTrace();
// }
// SharedPreferences sp=getSharedPreferences("userInfo", MODE_PRIVATE);
// SharedPreferences.Editor editor=sp.edit();
// editor.putString("userID",editText1.getText().toString());
// editor.putString("passwd",editText2.getText().toString());
// editor.commit();

// ContentValues values=new ContentValues();
// values.put("userID",editText1.getText().toString());
// values.put("password",editText2.getText().toString());
// db.insert("userInfo",null, values);
// db.close();
// values.clear();
SQLiteDatabase db=dbHelper.getWritableDatabase();
db.execSQL("insert into userInfo (userID,password) values(?,?)", new Object[]{editText1.getText().toString(), editText2.getText().toString()});
break;
// case R.id.login:
//////
//// break;Toast.makeText(MainActivity.this, "login button is clicked", Toast.LENGTH_LONG).show();
//// AlertDialog dialog =new AlertDialog.Builder(this)
//// .setTitle("Alert")
//// .setSingleChoiceItems(new String[]{"确定","取消"},0,null)
//// .setIcon(R.drawable.ic_launcher_foreground)
//// .create();
//// dialog.show();
//// Intent intent=new Intent();
//// intent.setAction(intent.ACTION_VIEW);
//// intent.setData(Uri.parse("https://cn.bing.com"));
//// startActivity(intent);
// break;
case R.id.image:
Intent intent1 = new Intent(this, Avator.class);
startActivityForResult(intent1, 1);
break;
case R.id.readdata:
// FileInputStream fis;
// String content="";
// try{
// fis= openFileInput("data.txt");
// byte [] buffer=new byte[fis.available()];
// fis.read(buffer);
// content=new String(buffer);
// String userid=content.split("##")[0];
// editText1.setText(userid);
// String passwd=content.split("##")[1];
// editText2.setText(passwd);
// }catch (Exception e){
// e.printStackTrace();
// }
// System.out.println(content);
// SharedPreferences sp1=getSharedPreferences("userInfo",MODE_PRIVATE);
// String userID=sp1.getString("userID","");
// String passwd=sp1.getString("passwd","");
// System.out.println(userID);
// editText1.setText(userID);
// editText2.setText(passwd);
// SQLite db = dbHelper.getWritableDatabase();
// System.out.println("zhiixngle");
SQLiteDatabase db2=dbHelper.getReadableDatabase();
int i=0,j=0;
Cursor cursor = db2.rawQuery("select userID,password from userInfo", null);
listItemCount=cursor.getCount();
if (cursor != null) {
while (cursor.moveToNext()) {
disUserID.add(cursor.getString(cursor.getColumnIndex("userID")));
disUserPasswd.add(cursor.getString(cursor.getColumnIndex("password")));
}
}
cursor.close();
db2.close();
userID=(String[])disUserID.toArray(new String[listItemCount]);
passwd=(String[])disUserPasswd.toArray(new String[listItemCount]);
MyBaseAdapter myBaseAdapter=new MyBaseAdapter(this);
listView.setAdapter(myBaseAdapter);
break;

case R.id.update:
SQLiteDatabase db3=dbHelper.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put("userID","0000");
db3.update("userInfo",contentValues,"userID=?",new String[]{""});
db3.close();
break;
case R.id.delete:
SQLiteDatabase db4=dbHelper.getWritableDatabase();
db4.delete("userInfo","userID=?",new String[]{"0000"});
db4.close();
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode) {
case 1:
int avatorRes = data.getIntExtra("avators", -1);
imageView.setImageResource(avatorRes);
break;
}
}


public class MyBaseAdapter extends BaseAdapter {
private LayoutInflater layoutInflater;

public MyBaseAdapter(Context context)
{
layoutInflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
return userID.length;
}

@Override
public Object getItem(int i) {
return userID[i];
}

@Override
public long getItemId(int i) {
return i;
}

@Override
public View getView(int i, View convertView, ViewGroup viewGroup) {
// System.out.println(i);
// System.out.println("userid"+userID[i]);
// View view=View.inflate(MainActivity.this,R.layout.data_display,null);
convertView=layoutInflater.inflate(R.layout.userinfo,null);
TextView textView4=(TextView) convertView.findViewById(R.id.dis_passwd);
TextView textView3=(TextView) convertView.findViewById(R.id.dis_ID);
textView3.setText(userID[i]);
textView4.setText(passwd[i]);
// System.out.println(textView3);
// editText3.setText(userID[i]);
// editText4.setText(passwd[i]);
// System.out.println("count"+listItemCount);
return convertView;
}
}
}
CATALOG
  1. 1. 结课考试复习