-
登录
public class ForinActivity extends AppCompatActivity {
private SQLiteDatabase db;
private Cursor cursor;
private EditText e1,e2;
private Button b1,b2;
private String u,p;
private Intent k;@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forin);
e1= (EditText) findViewById(R.id.editText4);
e2= (EditText) findViewById(R.id.editText3);
b1= (Button) findViewById(R.id.button9);
b2= (Button) findViewById(R.id.button10);b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { u=e1.getText().toString(); p=e2.getText().toString(); SQLiteHelper h = new SQLiteHelper(ForinActivity.this,"test",null,1); db = h.getReadableDatabase(); cursor=db.query("user2",new String[]{"name","password"},"name=? and password=?",new String[]{u,p},null,null,null); if(cursor.moveToFirst()) { Toast.makeText(ForinActivity.this,"登录成功",Toast.LENGTH_LONG).show(); Intent k=new Intent(ForinActivity.this,WelcomeActivity.class); startActivity(k); } else { Toast.makeText(ForinActivity.this,"登录失败",Toast.LENGTH_LONG).show(); } } });
2.注册
public class Main2Activity extends AppCompatActivity {
private Button b1,b2;
private EditText n1,p1;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
b1=(Button)findViewById(R.id.button7);
n1=(EditText)findViewById(R.id.editText);
p1=(EditText)findViewById(R.id.editText2);b2= (Button) findViewById(R.id.button8); b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SQLiteHelper h = new SQLiteHelper(Main2Activity.this,"test",null,1); db = h.getReadableDatabase(); String n_ame = n1.getText().toString(); String pasd = p1.getText().toString(); Cursor cursor = db.query("user2",new String[]{"name"},"name=?",new String[]{n_ame},null,null,null); if(cursor.getCount()>0) { Toast.makeText(getBaseContext(), "该用户名已经存在!!", Toast.LENGTH_SHORT).show(); return; } ContentValues values = new ContentValues(); values.put("name",n_ame); values.put("password",pasd); db.insert("user2",null,values); System.out.println("注册成功"); } });
3.定时跳转
public class WelcomeActivity extends AppCompatActivity {
// 定义一个跳转的意图Intent
private Intent i;
private TextView tv2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
tv2=(TextView) findViewById(R.id.textView2);
new CountDownTimer(10*1000,1000) {
@Override
public void onTick(long millisUntilFinished) {
//此处写“还剩X秒”代码
tv2.setText("还剩"+String.valueOf(millisUntilFinished/1000)+"秒");
}
@Override
public void onFinish() {
i = new Intent(WelcomeActivity.this,Main3Activity.class);
startActivity(i);
}
}.start();
4.碎片
public class Main3Activity extends AppCompatActivity {
private TextView tv1,tv2,tv3,tv4,tv5;
private FragmentManager fm;//碎片管理器
private FragmentTransaction ft;//碎片事务;什么是事务
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
tv1 = (TextView)findViewById(R.id.textView);
tv2 = (TextView)findViewById(R.id.textView2);
tv3 = (TextView)findViewById(R.id.textView3);
tv4 = (TextView)findViewById(R.id.textView4);
tv5 = (TextView)findViewById(R.id.textView5);
tv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
fm = getFragmentManager();
ft =fm.beginTransaction();//从这里事务
ft.replace(R.id.rame1,new F1Fragment());
ft.commit();//提交事务,到达这里
}
});
5.碎片Java
public class F1Fragment extends Fragment {
public F1Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstaneState) {
// Inflate the layout for this fragment
View v =inflater.inflate(R.layout.fragment_f1,null);
return v;
}
}
6.创建数据库
public class SQLiteHelper extends SQLiteOpenHelper {
static int VERSION=1;
public SQLiteHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table user(name char(20),password char(20))");
db.execSQL("create table user2(name string,password string)");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
}
}
7.数据链接
public class MainActivity extends AppCompatActivity {
private Button b1,b2,b3,b4,b5,b6;
public SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1 = (Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SQLiteHelper h = new SQLiteHelper(MainActivity.this,"test",null,1);
db = h.getReadableDatabase();
System.out.println("创建数据库成功");
}
});