Android期末复习重点代码,只是为了方便自己看,实在抱歉,没有标题党的意思
1.按钮绑定监听事件
<Button
android:id = "@+id/btn"
android:layout_width = "match_parent"
android:layout_height = "warp_content"
android:text = "按钮"/>
public class MainActivity extends Activity{
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
btn = (Button)findViewById(R.id.btn);
btn.setOnClickListener(new View.setOnClickListener){
@Override
public void onClick(View view){
btn.setText("按钮已经被点击");
}
}):
}
}
2.ListView代码
public class MainActivity extends Activity{
private ListView mListView;
private String[] titles{"桌子","苹果","蛋糕"};
private String[] prices{"600元","10元/斤","100元"};
private int[] icons{R.drawable.table,R.drawable.apple,R.drawable.cake};
@Override
protectde void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.MainActivity);
mListView = (ListView)findViewById(r.id.lv);
MyBaseAdapter adapter = new MyBaseAdapter();
mListView.setAadpter(adapter);
}
class MyBaseAdapter extends BaseAdapter{
@Override
public int getCount(){
return titles.length;
}
@Override
public Object getItem(int position){
return titles[position];
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position,View convertView,ViewGroup parent){
View view = view.inflate(MainActivity.this, R.layout.list_view, null);
TextView title = (TextView)findViewById(R.id.title);
TextView price = (TextView)findViewById(R.id.price);
ImageView iv = (ImageView)findViewById(R.id.iv);
title.setText(titles[position]);
price.setText(title[position]);
iv.setBackgroudResource(icons[position]);
return view;
}
}
}
3、Activity数据跳转以及回传
public class MainActivity extends Activity{
button1.setOnClickListener(new View.setOnClickListener){
@Override
public void onClick(View view){
Intent intent = new Intent();
intent.setClass(MainActivity.this,SecondActivity.class);
startActivityForReuslt(intent,1);
}
@Override
public void onAcitivityResult(int requestCode,int responseCode,Intent intent){
super.onAcitivityResult(requestCode,responseCode,intent);
if(requestCode==200 && responseCode==200){
String name = intent.getStringExtra("name");
int score = intent.getIntExtra("score");
}
}
}
public class SecondActivity extends Activity{
button2.setOnClickListener(new View.setOnClickListener){
@Override
public void onClick(View view){
Intent intent = new Intent();
intent.putExtra("name","lisi");
intent.putExtra("score",98);
startActivity(intent);
setResult(2,intent);
finish();
}
}
4、数据存储方式
4.1、文件存储
4.1.1内部存储
(存数据代码。路径data/data/<packagename>/ 目录下)
String filename = "data.txt";
FileOutputStream fos = null;
String content = "helloworld";
try{
fos = openFileOutput(filename,MODE_PRIVATE);
fos.write(conten.getBytes());
}catch(Exception e){
e.printStackTrace();
}finally{
fos.close();
}
(读取代码)
String content = "";
FileInputStream fis = null;
try{
fis = openFileInput("data.txt");
byte[] buffer = new byte[fis.avaliable];
fis.read(buffer);
content = new String(buffer);
}catch{
e.printStackTrace();
}finally{
fis.close();
}
4.1.2 外部SD卡存储
(写代码)
String state = Environment.getExternalStroageState();
if(state.equals(Environment.MEDIA_MOUNTED)){
File SDPath = Environment.getExternalStroageDirectory();
File file = new File(SDPath,"data.txt");
String data = "helloworld";
FileOutputStream fos = null;
try{
fos = openFileOutput(filename,MODE_PRIVATE);
fos.write(conten.getBytes());
}catch(Exception e){
e.printStackTrace();
}finally{
fos.close();
}
}
(读取代码)
String state = Environment.getExternalStroageState();
if(state.equals(Environment.MEDIA_MOUNTED)){
File SDPath = Environment.getExternalStroageDirectory();
File file = new File(SDPath,"data.txt");
FileInputStream fis = null;
BufferReader br = null;
try{
fis = new FileInputStream(file);
br = new BufferReader(new InputStreamReader(fis));
String data = br.readLine();
}catch(Exception e){
e.printStackTrace();
}finally{
fos.close();
}
}
4.2、SharedPerferences存储
(存代码)
SharedPerferences sp getSharedPerferences("data",MODE_PRIVATE);
SharedPerferences.Editor editor = sp.edit();
editor.putString("name","lisi");
editor.putInt("age",18);
editor.commit();
(读取代码)
SharedPerferences sp getSharedPerferences("data",MODE_PRIVATE);
String data = sp.getString("name","");
(删除)
SharedPerferences sp getSharedPerferences("data",MODE_PRIVATE);
SharedPerferences.Editor editor = sp.edit();
editor.remove("name");
editor.clear();
4.3、SQLite
创建数据库
public class MyHelper extends SQLiteDatabse{
public MyHelper(Context context){
super(conten, "itcast.db", null, 2);
}
public void onCreate(SQLiteDatabse db){
db.execSQL("create table information(_id Integer primary key AutoIncrement, name Varchar(20), price Integer)");
}
public void onUpgrade(SQLiteDatabse db,int oldVersion, int newVersion){
}
}
增加数据
public void insert(String name,int price){
MyHelper helper = new MyHelper(MainActivity.this);
SQLiteDatabse db = helper.getWritableSqlDatabse();
db.execSQL("insert into information(name,price) values(?,?)", new Object[]{name,price});
db.close();
}
修改数据
public void update(String name,long id){
MyHelper helper = new MyHelper(MainActivity.this);
SQLiteDatabse db = helper.getWritableSqlDatabse();
db.execSQL("update information set name=? where id =?",new Object[]{name ,id});
db.close();
}
删除数据
public void delete(long id){
MyHelper helper = new MyHelper(MainActivity.this);
SQLiteDatabse db = helper.getWritableSqlDatabse();
db.execSQL("delete from information where id =?",new Object[]{id});
db.close();
}
查询数据
public void select(String name){
MyHelper helper = new MyHelper(MainActivity.this);
SQLiteDatabse db = helper.getWritableSqlDatabse();
Cursor cursor = db.rawQuery(select * from information where name =?",new Object[]{name})
if(cursor.getCount!=0){
where(cursor.moveNoText()){
String id = cursor.getString(cursor.getColumnIndec("_id"));
String score = cursor.getString(cursor.getColumnIndec("score"));
}
}
cusor.close();
db.close();
}
|