Fragment复习
重点布局
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v4.view.ViewPager
android:id="@+id/vp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<TextView
android:id="@+id/daojishi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="倒计时"
android:textColor="#000"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:textSize="20sp"
android:visibility="gone"/>
<Button
android:id="@+id/jump"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击跳转"
android:layout_centerHorizontal="true"
android:layout_marginTop="400dp"
android:visibility="gone"/>
</RelativeLayout>
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main2Activity">
<android.support.design.widget.TabLayout
android:id="@+id/tl"
android:layout_width="match_parent"
android:layout_height="50dp">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:orientation="vertical"
android:id="@+id/vp"
android:layout_below="@+id/tl"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
</RelativeLayout>
主要代码(Fragment代码省略)
package com.example.day1014homework;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.example.day1014homework.MainFragment.OneFragment;
import com.example.day1014homework.MainFragment.ThreeFragment;
import com.example.day1014homework.MainFragment.TwoFragment;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
private ViewPager vp;
private TextView daojishi;
private Button jump;
private List<Fragment> fragmentList;
private Timer timer1 = new Timer();
private Timer timer2 = new Timer();
private int index = 0;
private int time = 6;
Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if (msg.what == 100){
vp.setCurrentItem(index);
index++;
if (index == fragmentList.size()){
daojishi.setVisibility(View.VISIBLE);
jump.setVisibility(View.VISIBLE);
timer1.cancel();
timer2.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(101);
}
},0,1000);
}
}
if (msg.what == 101){
daojishi.setText("倒计时"+(--time)+"秒");
if (time == 0){
timer2.cancel();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
}
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
vp = (ViewPager) findViewById(R.id.vp);
daojishi = (TextView) findViewById(R.id.daojishi);
jump = (Button) findViewById(R.id.jump);
fragmentList = new ArrayList<>();
OneFragment oneFragment = new OneFragment();
TwoFragment twoFragment = new TwoFragment();
ThreeFragment threeFragment = new ThreeFragment();
fragmentList.add(oneFragment);
fragmentList.add(twoFragment);
fragmentList.add(threeFragment);
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
});
timer1.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(100);
}
},0,1000);
jump.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
timer2.cancel();
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
finish();
}
});
}
}
package com.example.day1014homework;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.LinearLayout;
import com.example.day1014homework.Main2Fragment.Blank1Fragment;
import com.example.day1014homework.Main2Fragment.Blank2Fragment;
import com.example.day1014homework.Main2Fragment.Blank3Fragment;
import java.util.ArrayList;
import java.util.List;
public class Main2Activity extends AppCompatActivity {
private TabLayout tl;
private ViewPager vp;
private List<Fragment> fragmentList = new ArrayList<>();
private List<String> page = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tl = (TabLayout) findViewById(R.id.tl);
vp = (ViewPager) findViewById(R.id.vp);
Blank1Fragment blank1Fragment = new Blank1Fragment();
Blank2Fragment blank2Fragment = new Blank2Fragment();
Blank3Fragment blank3Fragment = new Blank3Fragment();
fragmentList.add(blank1Fragment);
fragmentList.add(blank2Fragment);
fragmentList.add(blank3Fragment);
for (int i = 0; i < 3; i++) {
page.add("第"+(i+1)+"页");
}
vp.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(int i) {
return fragmentList.get(i);
}
@Override
public int getCount() {
return fragmentList.size();
}
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return page.get(position);
}
});
tl.setupWithViewPager(vp);
}
}
Sqlite练习
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/zeng"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增加"
android:onClick="click"/>
<Button
android:id="@+id/shan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="click"/>
<Button
android:id="@+id/gai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"
android:onClick="click"/>
<Button
android:id="@+id/cha"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"
android:onClick="click"/>
</LinearLayout>
创建一个类继承 SQLiteOpenHelper
package com.example.app2.sql;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class MySqlHelp extends SQLiteOpenHelper {
public MySqlHelp(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student (id integer primary key autoincrement,name varchar(20),address varchar(20))");
db.beginTransaction();
for (int i = 0; i < 10; i++) {
db.execSQL("insert into student values(null,'过江龙遇下山虎','小伙不由你做主')");
}
db.setTransactionSuccessful();
db.endTransaction();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
增删改查的代码(两种方法)
package com.example.app2;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
import com.example.app2.sql.MySqlHelp;
public class MainActivity extends AppCompatActivity {
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySqlHelp mySqlHelp = new MySqlHelp(this, "person.db", null, 1);
db= mySqlHelp.getReadableDatabase();
}
public void click(View view) {
switch (view.getId()){
case R.id.zeng:
String sql = "insert into student(name,address) values (?,?)";
db.execSQL(sql,new Object[]{"大金牙","上海滩"});
break;
case R.id.shan:
String sql2 ="delete from student where id = ?";
db.execSQL(sql2,new Object[]{2});
break;
case R.id.gai:
ContentValues contentValues = new ContentValues();
contentValues.put("address","不知道");
db.update("student",contentValues,"id=?",new String[]{"1"});
break;
case R.id.cha:
Cursor cursor = db.query("student", null, null, null, null, null, null);
if (cursor != null){
while (cursor.moveToNext()){
String id = cursor.getString(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String address = cursor.getString(cursor.getColumnIndex("address"));
Toast.makeText(this, id+"--"+name+"--"+address, Toast.LENGTH_SHORT).show();
}
}
cursor.close();
break;
}
}
}