Dialog组件
在Activity中用户可以主动调用的函数是:
showDialog(int id),负责显示标识为id的Dialog,函数如果被调用, 会触发onCreateDialog(int id)
dismissDialog(int id),使对应Dialog在界面中消失
Dialog有两个常见的触发方法:onCreateDialog(int id)和onPrepareDialog(int id,Dialog dialog).当调用了showDialog(int id),如果这个Dialog是第一次生成,则触发onCreateDialog(int id),然后再调用onPrepareDialog(int id,Dialog dialog);(该方法提供了当Dialog生成还没有显示出来的时候,有机会在显示前对Dialog做一些修改的功能。)
生成Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context)
builder.setIcon(R.drawable.xxx);
builder.setTitle(R.string.xxx);
builder.setPositiveButton(R.string.xxx,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
...
}
});
builder.setNegativeButton(R.string.xxx,...);
builder.create();
//给Dialog设置一个View
//通过inflate方法可以将一个xml布局变成一个View实例
LayoutInflater inflater = LayoutInflater.from(Context context);
Final View xx = inflater.inflate(R.layout.xxx,null);
...
Builder.setView(xx);
ProgressDialog 负责显示进度的相关情况,是AlertDialog的一个子类:
ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle(“xx”);
dialog.setMessage(“xxx”);
Android数据存储操作
非共享的文件系统
存储方式有:SharedPreference、文件存储、SQLite数据库方式、Content Provider 和网络。
1.SharedPreferences 用来存储简单的配置信息,如欢迎语、登录的用户名和密码等,相当于cookies
SharedPreferences settings= getSharedPreference(SETTING_Infos,0)
//提取SharedPreference中的数据
String name = settings.getString(“NAME”, “”);
String password = settings.getString(“PASSWORD”,””);
field_name.setText(name);
field_password.setText(password);
protected void onStop(){ //onStop()就是程序退出时
//再将文本框中的数据存进SharedPreference
}
2.输入输出流
FileOutputStream fos = openFileOutPut(...);
FileInputStream fis = openFileInput(filename);
如果需要一些额外的资源,可以将文件放在/res/raw下,这样可以使用getResources获取资源,用openRawResource方法(不带后缀的文件名)打
开文件:Resource myRes = getResource();
InputStream myFile = myRes.openResource(R.raw.xxx);