12. 常见控件(二)
RadioGroup, RadioButton 和 Toast
<RadioGroup
android:id="@+id/genderGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<RadioButton
android:id="@+id/femaleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female" />
<RadioButton
android:id="@+id/maleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male"
/>
</RadioGroup>
private RadioGroup genderGroup = null;
private RadioButton femaleButton = null;
genderGroup = (RadioGroup) findViewById(R.id.genderGroup);
femaleButton = (RadioButton) findViewById(R.id.femaleButton);
maleButton = (RadioButton) findViewById(R.id.maleButton);
genderGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (femaleButton.getId() == checkedId) {
System.out.println("female___2");
Toast.makeText(Activity07.this, "female___3", Toast.LENGTH_SHORT).show();
} else if (maleButton.getId() == checkedId) {
System.out.println("maiel ");
}
}
});
CheckBox
<CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/swim" />
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/run" />
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/read" />
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
swimBox = (CheckBox) findViewById(R.id.swim);
runBox = (CheckBox) findViewById(R.id.run);
readBox = (CheckBox) findViewById(R.id.read);
swimBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("swim is checked ");
} else {
System.out.println("swim is not checked ");
}
}
});
runBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("run is checked ");
} else {
System.out.println("run is not checked ");
}
}
});
readBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
System.out.println("read is checked ");
} else {
System.out.println("read is not checked ");
}
}
});
13. 常见控件(三)
ProgressBar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ProgressBar
android:id="@+id/firstBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<ProgressBar
android:id="@+id/secondBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<Button
android:id="@+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="begin"
/>
</LinearLayout>
public class ProgressBarTestActivity extends Activity {
int i=0;
private ProgressBar firstBar = null;
private ProgressBar secondBar = null;
private Button myButton = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
firstBar = (ProgressBar) findViewById(R.id.firstBar);
secondBar = (ProgressBar) findViewById(R.id.secondBar);
myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(new ButtonListener());
}
class ButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
if (i == 0) {
firstBar.setVisibility(View.VISIBLE);
firstBar.setMax(150);
secondBar.setVisibility(View.VISIBLE);
} else if (i < firstBar.getMax()) {
firstBar.setProgress(i);
firstBar.setSecondaryProgress(i + 10);
secondBar.setProgress(i);
} else {
firstBar.setVisibility(View.GONE);
secondBar.setVisibility(View.GONE);
}
i = i+10;
}
}
}
ListView
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/listLinearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:drawSelectorOnTop="false"
android:scrollbars="vertical" />
</LinearLayout>
</LinearLayout>
user.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:paddingBottom="1dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:paddingTop="1dip" >
<TextView
android:id="@+id/user_name"
android:layout_width="180dip"
android:layout_height="30dip"
android:singleLine="true"
android:textSize="10pt" />
<TextView
android:id="@+id/user_ip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="right"
android:textSize="10pt" />
</LinearLayout>
package mars.myListView;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MyListView extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> map1 = new HashMap<String, String>();
HashMap<String,String> map2 = new HashMap<String, String>();
HashMap<String,String> map3 = new HashMap<String, String>();
map1.put("user_name", "zhangsan");
map1.put("user_ip", "192.168.0.1");
map2.put("user_name", "lisi");
map2.put("user_ip", "192.168.0.2");
map3.put("user_name", "wangwu");
map3.put("user_ip", "192.168.0.3");
list.add(map1);
list.add(map2);
list.add(map3);
SimpleAdapter listAdapter = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name", "user_ip"}, new int[]{R.id.user_name, R.id.user_ip});
setListAdapter(listAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
System.out.println("id----------" + id);
System.out.println("position----" + position);
}
}
14. Handler的使用(一)
public class HandlerTestActivity extends Activity {
private Button startButton = null;
private Button endButton = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.startButton);
startButton.setOnClickListener(new StartButtonListener());
endButton = (Button) findViewById(R.id.endButton);
endButton.setOnClickListener(new EndButtonListener());
}
class StartButtonListener implements OnClickListener {
@Override
public void onClick(View arg0) {
handler.post(updateThread);
}
}
class EndButtonListener implements OnClickListener {
@Override
public void onClick(View v) {
handler.removeCallbacks(updateThread);
}
}
Handler handler = new Handler();
Runnable updateThread = new Runnable() {
@Override
public void run() {
System.out.println("UpdateThread");
handler.postDelayed(updateThread, 3000);
}
};
}
15. Handler的使用(二)
Handler和Listenr与Activity在同一线程里,用了Looper机制
HandlerThread用于创建一个新线程,新线程中也使用Looper机制
msg.arg1和msg.arg2用于传递简单整数
msg.bundle用于进程内传递多个任意类型数据
msg.object用于在进程间传递无类型数据
16. SQLite使用方法
SQLiteOpenHelper
adb 访问SQLite
ContentValues
Cursor
17. debug
Log.w("myTag","my log .... ");
Log.d("myTag","my log .... ");
Log.i("myTag","my log .... ");
Log.e("myTag","my log .... ");
Log.v("myTag","my log .... ");