开卷考,直接省去减少要记要背的负担
选择题
上一年还有的,今年没了
填空题
平常作业的代码,多看老师的代码
大题
手写几个实现代码,有broadcastReceiver,Intent,EditText,Layout
设计题
20分,占五分之一面值,是写华为备忘录界面,还有个+创建笔记的过程,试卷上是列表视图
我是想到啥写啥,笔记到待办两个Intent之间来回的过程,ListView,SQLite搜索还有长按出现删除的longClick事件监听一堆差不多写满了一大页,考试时长一个半,感觉分配有点短。短暂的安卓开发应该算是结束了,可能算是下一步的起点吧。
同学整理的笔记
contentprovider
public class StudentsProvider extends ContentProvider {
static final String PROVIDER_NAME = "com.example.provider.College";
static final String URL = "content://" + PROVIDER_NAME + "/students";
static final Uri CONTENT_URI = Uri.parse(URL);
static final String _ID = "_id";
static final String NAME = "name";
static final String GRADE = "grade";
private static HashMap<String, String> STUDENTS_PROJECTION_MAP;
static final int STUDENTS = 1;
static final int STUDENT_ID = 2;
static final UriMatcher uriMatcher;
static {
uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
uriMatcher.addURI(PROVIDER_NAME, "students", STUDENTS);
uriMatcher.addURI(PROVIDER_NAME, "students/#", STUDENT_ID);
}
private SQLiteDatabase sqLiteDatabase;
static final String DATABASE_NAME = "College";
static final String STUDENTS_TABLE_NAME = "students";
static final int DATABASE_VERSION = 1;
static final String CREATE_DB_TABLE =
" CREATE TABLE " + STUDENTS_TABLE_NAME +
" (_id INTEGER PRIMARY KEY AUTOINCREMENT, " +
" name TEXT NOT NULL, " +
" grade TEXT NOT NULL);";
private static class Student_Database extends SQLiteOpenHelper {
Student_Database(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
db.execSQL("DROP TABLE IF EXISTS " + STUDENTS_TABLE_NAME);
onCreate(db);
}
}
@Override
public boolean onCreate() {
Context context = getContext();
Student_Database dbHelper = new Student_Database(context);
sqLiteDatabase = dbHelper.getWritableDatabase();return sqLiteDatabase != null;
}
@Override
public Uri insert(Uri uri, ContentValues values) {
long rowID = sqLiteDatabase.insert(STUDENTS_TABLE_NAME, "", values);
if (rowID > 0) {
//withAppendedId这个方法负责把rowID和CONTENT_URI连接成一个新的Uri
Uri _uri = ContentUris.withAppendedId(CONTENT_URI, rowID);
getContext().getContentResolver().notifyChange(_uri, null);
return _uri;
}
}
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[]
selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
qb.setTables(STUDENTS_TABLE_NAME);
switch (uriMatcher.match(uri)) { // 根据返回值可以判断这次查询请求,它是请求全
部数据还是某个id的数据
case STUDENTS:
qb.setProjectionMap(STUDENTS_PROJECTION_MAP);
break;
case STUDENT_ID:
qb.appendWhere(_ID + "=" + uri.getPathSegments().get(1));
break;
}
if (sortOrder == null || sortOrder.equals("")) {
sortOrder = NAME;
}
//查询是SQLiteQueryBuilder来发起的,而不是SQLiteDatabase 直接发起的,所以在参数
方面略有不同
Cursor c = qb.query(sqLiteDatabase, projection, selection,
selectionArgs, null, null, sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int delete(Uri uri, String selection, String[] selectionArgs) {
int count;
switch (uriMatcher.match(uri)) {
case STUDENTS:
count = sqLiteDatabase.delete(STUDENTS_TABLE_NAME, selection,
selectionArgs);
break;
case STUDENT_ID:
String id = uri.getPathSegments().get(1);
count = sqLiteDatabase.delete(STUDENTS_TABLE_NAME, _ID + " = " +
id +
(!TextUtils.isEmpty(selection) ? " AND (" + selection +
')' : ""), selectionArgs);
break;
}
getContext().getContentResolver().notifyChange(uri, null);return count;
}
@Override
public int update(Uri uri, ContentValues values, String selection, String[]
selectionArgs) {
int count;
switch (uriMatcher.match(uri)) {
case STUDENTS:
count = sqLiteDatabase.update(STUDENTS_TABLE_NAME, values,
selection, selectionArgs);
break;
case STUDENT_ID:
count = sqLiteDatabase.update(STUDENTS_TABLE_NAME, values, _ID +
" = " + uri.getPathSegments().get(1) +
(!TextUtils.isEmpty(selection) ? " AND (" + selection +
')' : ""), selectionArgs);
break;
}
getContext().getContentResolver().notifyChange(uri, null);
return count;
}
@Override
public String getType(Uri uri) {
switch (uriMatcher.match(uri)) {
case STUDENTS:
return "vnd.android.cursor.dir/vnd.example.students";
case STUDENT_ID:
return "vnd.android.cursor.item/vnd.example.students";
}
}
}
// fun MainActivity
public void onClickAddName(View view) {
// Add a new student record
ContentValues values = new ContentValues();
values.put(StudentsProvider.NAME,
((EditText)findViewById(R.id.editText2)).getText().toString());
values.put(StudentsProvider.GRADE,
((EditText)findViewById(R.id.editText3)).getText().toString());
Uri uri = getContentResolver().insert(
StudentsProvider.CONTENT_URI, values);
Toast.makeText(getBaseContext(),
uri.toString(), Toast.LENGTH_LONG).show();
}
public void onClickRetrieveStudents(View view) {
String URL = "content://com.example.provider.College/students";Uri students = Uri.parse(URL);
Cursor c = getContentResolver().query(students,null,null,null,"name");
if (c.moveToFirst()) {
do{
Toast.makeText(this,
c.getString(c.getColumnIndex(StudentsProvider._ID)) +
", " +
c.getString(c.getColumnIndex(StudentsProvider.NAME)) +
", " +
c.getString(c.getColumnIndex(StudentsProvider.GRADE)),
Toast.LENGTH_SHORT).show();
} while (c.moveToNext());
}
}
<Button
android:id="@+id/button2"
android:onClick="onClickAddName"
android:text="Add Name" />
<EditText
android:id="@+id/editText2"
android:hint="Name"/>
<EditText
android:id="@+id/editText3"
android:hint="Grade" />
<Button
android:id="@+id/button"
android:onClick="onClickRetrieveStudents"
android:text="Retrive student" />
存储
外部存储
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE " />
内部存储
FileOutputStream openFileOutput(String name,int mode);
MODE_PRIVATE: 默认的操作权限,只能被当前应用程序所读写。
MODE_APPEND: 可以添加文件的内容。
MODE_WORLD_READABLE: 可以被其他程序所读取,安全性较低。
MODE_WORLD_WRITEABLE:可以被其他的程序所写入,安全性低。
FileInputStream openFileInput(String name);
SharedPreferences
// 保存数据可以参考如下代码
SharedPreferences.Editor editor =
getSharedPreferences("mydata",MODE_PRIVATE).edit();
editor.putString("key1","value1");
editor.putFloat("key2",2);
edit.commit();
// 读取数据可以参考如下代码
SharedPreferences sp = getSharedPreferences("mydata",MODE_PRIVATE);
String value1 = sp.getString("key1","");
float value2 = sp.getFloat("key2",0f);
menu
<!-- res/menu/main_menu.xml -->
<item android:title="Add" />
<item android:title="Remove" />
// fun onCreateOptionsMenu
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);
return true;
// fun onOptionsItemSelected
if(item.getItemId() == R.id.add_item){
// Do something
}
return true;
finish
finish(); // 销毁 Activity
MainActivity.this.isFinishing();
Intent
显式
// FirstActivity.java
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
intent.putExtra("extra_data", "Hello SecondActivity");
startActivity(intent);
// SecondActivity.java
Intent intent = getIntent();
String message = intent.getStringExtra("extra_data");
// FirstActivity.java
// Intent intent = getIntent();
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("data_return", "Hello FirstActivity");
setResult(RESULT_OK, intent);
// SecondActivity.java
// fun onActivityResult
if (RESULT_OK == requestCode) {
String returnedData = data.getStringExtra("data_return");
}
// SecondActivity.java
// fun actionStart(Context context, String data1, String data2)
Intent intent = new Intent(context, SecondActivity.class);
intent.putExtra("param1", data1);
intent.putExtra("param2", data2);
context.startActivity(intent);
// FirstActivity.java
SecondActivity.actionStart(MainActivity.this, "data1", "data2");
override fun onBackPressed() {
}
override fun onDestroy(){
super.onDestroy()
}
隐式
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("https://www.google.com"));
intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
界面布局
android:text="text"
android:textAllCaps="false"
android:textSize="25sp"
android:gravity="center"
android:Color=""#ff0000"
<!-- EditText -->
android:hint="提示"
<!-- item -->
android:title="菜单项" />
<!-- RadioButton -->
<RadioGroup>
android:orientation="horizontal" <!-- vertical -->
<RadioButton
android:checked="true"/>
<RadioButton/>
</RadioGroup>
<!-- ConstraintLayout -->
android:layout_weight="1"
android:layout_marginRight="22dp"
<!-- RelativeLayout -->
android:layout_toRightOf="@+id/txt1"
android:layout_alignTop="@+id/txt2"