1.样式和主题
1:可以在res -> values -> styles.xml中定义公共的样式或者主题。
2:主题作用在整个应用,可以在AndroidManifest.xml的application节点下使用。
<style name="MyTextView" >
- "android:layout_width"
>wrap_content
- "android:layout_height">wrap_content
- "android:textColor">#ff0000
- "android:textSize">20sp
- "android:background">#66000000
style>
<TextView
style="@style/MyTextView"
android:text="hello_world" />
<style name="MyTheme">
- "android:background"
>#66000000
style>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
application>
2.国际化
1:在res文件夹下创建values-zh目录,然后编写strings.xml文件,将需要的文件进行翻译之后语言的匹配。
2:在代码中如果涉及字符串内容,使用R.string代替。
3:在布局文件中如果涉及字符串,使用@string/代替。
<resources>
<string name="app_name">26-------string>
<string name="action_settings">设置string>
<string name="hello_world">你好string>
resources>
<TextView
style="@style/MyTextView"
android:text="@string/hello_world" />
3.对话框
public class MainActivity extends Activity {
private boolean[] checkedItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void normal(View v){
Builder builder = new Builder(this);
builder.setTitle("普通对话框");
builder.setMessage("丰富多彩的普通对话框的内容");
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "确定1", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "取消1", Toast.LENGTH_SHORT).show();
}
});
builder.show();
}
public void singleChoice(View v){
Builder builder = new Builder(this);
builder.setTitle("单选");
final String[] items = {"11","22","33","44","55"};
builder.setSingleChoiceItems(items, 3, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
builder.show();
}
public void multiChoice(View v){
Builder builder = new Builder(this);
builder.setTitle("多选框");
final String[] items = {"111","222","333","444","555"};
checkedItems = new boolean[] {true,false,true,false,true};
builder.setMultiChoiceItems(items, checkedItems, new OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
checkedItems[which] = isChecked;
for(int i = 0;i < items.length;i++){
if(checkedItems[i]){
System.out.println("选中" + items[i] + checkedItems[which]);
}
}
}
});
builder.setPositiveButton("确认", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
public void progress(View v){
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setTitle("下载数据");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setMax(100);
progressDialog.show();
new Thread(){
public void run() {
for(int i = 0;i < 100;i++){
progressDialog.setProgress(i);
SystemClock.sleep(100);
}
progressDialog.show();
}
}.start();
}
}
4.关于getApplicationContext()
1:getApplicationContext()可以使用的大多数的地方,但是不能再对话框中使用。因为对话框要显示在当前的activity中,是activity的一部分,而getApplicationContext()是整个应用上下文,所以应该传入当前的activity,因为他不能脱离当前的activity,可传入this。
2:Toast.makeText()方法在使用的使用可以传入getApplicationContext(),因为Toast.makeText()可以理解为一个单独的组件,不用必须游离在当前activity的上方,即他可以脱离当前的activity。
5.Activity
1:Android中每一个页面都是一个Activity。
2:创建Activity的步骤,写一个类继承Activity,重写onCreate()方法。
3:onCreate()方法是Activity创建后调用的第一个方法,在这个方法中可以进行初始化的操作。
4:setContentView(R.layout.activity_main)方法,可以将布局文件转化为view对象并显示。
5:可以使用findViewById(R.id.**),找到相关的组件。
6:加载数据。getCacheDir();获取/data/data/包名/Cache 这个路径。getFilesDir();获取 /data/data/包名/Files这个路径。openFileInput("文件名")操作getFilesDir()目录下的文件,返回一个输入流。openFileOutput("文件名", mode);操作getFilesDir()目录下的文件,返回一个输出流。Environment.getexternalStorageDirectory();获取sd卡路径。
6.xml保存数据
SharedPreferences sp = getSharedPreferences("info", MODE_PRIVATE);
String name = sp.getString("name", "bob");
Editor edit = sp.edit();
edit.putString("name", "bob");
edit.putString("name", "tom");
edit.commit();
7.数据库-SQLite
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context) {
super(context, "data.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table info(_id integer " +
"primary key autoincrement,name " +
"varchar(20),phone varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MySQLiteOpenHelper myOpenHelper = new MySQLiteOpenHelper(this);
SQLiteDatabase readableDatabase = myOpenHelper.getReadableDatabase();
SQLiteDatabase writableDatabase = myOpenHelper.getWritableDatabase();
readableDatabase.execSQL("insert into info(name,phone) values('bob','111)");
ContentValues cv = new ContentValues();
cv.put("name", "tom");
long result = readableDatabase.insert("info", "name", cv);
}
8.网络编程
1:不能在主线程中进行联网操作。
2:子线程不能修改UI。
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
System.out.println(msg.obj);
}
};
public void onClick(View v){
new Thread(){
public void run() {
try {
URL url = new URL("path");
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(10000);
int code = connection.getResponseCode();
if(code == 200){
InputStream inputStream = connection.getInputStream();
String data = "数据";
Message obtain = Message.obtain();
obtain.what = 1;
obtain.obj = data;
handler.sendMessage(obtain);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
connection.disconnect();
}
}
}.start();
}
9.Intent
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String n = intent.getStringExtra("name");
intent.putExtra("name", "bob");
}
10.EditText和TextView
1:通过findViewById(R.id.**)获取组件。
2:**.getText().toString();获取组件的内容。
3:**.setText("字符串");为其设置内容。
11.ListView
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.lv_list);
String[] objects = {"张三","lisi","wangwu","zhaoliu","fengti"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.item1, objects);
adapter = new ArrayAdapter<String>(this, R.layout.item2, R.id.tv_text, objects);
listView.setAdapter(adapter);
}
public class MainActivity extends Activity {
private MyOperHelper myOperHelper;
private List<Info> list = new ArrayList<Info>();
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.lv_view);
myOperHelper = new MyOperHelper(this);
}
public void insert(View v){
SQLiteDatabase database = myOperHelper.getReadableDatabase();
database.execSQL("insert into info(name,phone) values('王五','13777777')");
database.execSQL("insert into info(name,phone) values('zhaoliu','111111')");
database.execSQL("insert into info(name,phone) values('tianqi','122222')");
database.close();
}
public void query(View v){
SQLiteDatabase database = myOperHelper.getReadableDatabase();
Cursor cursor = database.rawQuery("select * from info", null);
while(cursor.moveToNext()){
Info info = new Info();
info.name = cursor.getString(1);
info.phone = cursor.getString(2);
list.add(info);
}
for(Info info : list){
System.out.println(info);
}
listView.setAdapter(new MyAdapter());
}
public class MyAdapter extends BaseAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if(convertView == null){
view = View.inflate(MainActivity.this, R.layout.item, null);
}else {
view = convertView;
}
TextView tv_name = (TextView) view.findViewById(R.id.tv_name);
TextView tv_phone = (TextView) view.findViewById(R.id.tb_phone);
tv_name.setText(list.get(position).name);
tv_phone.setText(list.get(position).phone);
return view;
}
}
}
12.打开另一个Activity
public void onClick1(View v){
Intent intent = new Intent();
intent.setAction("com.my.SecondActivity");
intent.setData(Uri.parse("tel:" + 111));
startActivity(intent);
}
13.打开一个Activity之后,取出数据+返回
public void onClick2(View v){
Intent intent = new Intent();
intent.setAction("com.my.SecondActivity");
intent.setData(Uri.parse("tel:" + 111));
startActivityForResult(intent, 1);
}