1.ListView 使用
该控件就像Android中的系统设置那样
ListView lv;
//定义数据适配器
ArrayAdapter<String> adapter;
//数据适配器的数据
String []str={"网络设置","其它方式","语言设置"};
//从xml文件中得到ListView控件
lv=(ListView) findViewById(R.id.list);
//初始化数据适配器
adapter=new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1,str);
//Listview 设置数据适配器
lv.setAdapter(adapter);
//设置lv的监听点击事件
lv.setOnItemClickListener(new OnItemClickListener(){})
2.Dialog对话框
//普通对话框
AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
dialog.setTitle("相对复杂的对话框").setIcon(R.drawable.ic_launcher)
.setPositiveButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).setNegativeButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).create();
dialog.show();
//转到等待对话框
ProgressDialog pdg=new ProgressDialog(MainActivity.this);
pdg.setMessage("loading...");
pdg.setTitle("弹出转动条对话框");
pdg.show();
android弹出单选、多选菜单
https://blog.csdn.net/meizhen51/article/details/6155912
菜单弹出窗口
//上面图的实现
private String[] areas = new String[]{"全部","玉兰香苑", "张江地铁站", "金科路", "张江路", "紫薇路", "香楠小区" };
new AlertDialog.Builder(MainActivity.this).setTitle("选择区域").setItems(areas,new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
Toast.makeText(MainActivity.this, "您已经选择了: " + which + ":" + areas[which],Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}).show();
单选弹出菜单窗口
private String[] areas = new String[]{"全部","玉兰香苑", "张江地铁站", "金科路", "张江路", "紫薇路", "香楠小区" };
private RadioOnClick radioOnClick = new RadioOnClick(1);
ListView areaRadioListView;
AlertDialog ad =new AlertDialog.Builder(MainActivity.this).setTitle("选择区域")
.setSingleChoiceItems(areas,radioOnClick.getIndex(),radioOnClick).create();
areaRadioListView=ad.getListView();
ad.show();
/**
* 点击单选框事件
* @author xmz
*
*/
class RadioOnClick implements DialogInterface.OnClickListener{
private int index;
public RadioOnClick(int index){
this.index = index;
}
public void setIndex(int index){
this.index=index;
}
public int getIndex(){
return index;
}
public void onClick(DialogInterface dialog, int whichButton){
setIndex(whichButton);
Toast.makeText(MainActivity.this, "您已经选择了: " + index + ":" + areas[index], Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}
//时间选择对话框
Calendar c2=Calendar.getInstance();
TimePickerDialog tpg=new TimePickerDialog(MainActivity.this, new OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
// TODO Auto-generated method stub
//得到你所选择的时间
Toast.makeText(getApplicationContext(), String.valueOf(hourOfDay)+
":"+String.valueOf(minute), Toast.LENGTH_LONG).show();
}
}, c2.get(Calendar.HOUR), c2.get(Calendar.MINUTE),true);
tpg.show();
3.开关与按钮
<ToggleButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="on"
android:textOff="off"
android:checked="false"
android:id="@+id/tbn"/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="on"
android:textOff="off"
android:id="@+id/sih"
android:track="@drawable/ic_launcher" 设置底部背景的图片
android:thumb="@drawable/ic_launcher" 设置滑块的图片
/>
android中的isChecked()方法,上面那两个控件的对象可调用.它用来判断控件是否选中,返回值为布尔值.
4.进度条
水平进度条
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100" 设置进度条的最大值
android:progress="49" 指定进度值
/>
5.WebView控件(可认为在自己开发的应用嵌入一个浏览器)
//增加网络权限
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="@+id/edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.96" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="seartch"/>
</LinearLayout>
<WebView
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/web"
android:layout_weight="1.01" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent">
<Button
android:layout_marginLeft="70dp"
android:id="@+id/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="《"/>
<Button
android:id="@+id/right"
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="》"/>
</LinearLayout>
</LinearLayout>
public class MainActivity extends Activity implements View.OnClickListener{
WebView web;
Button button,button_left,button_right;
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_left=(Button)findViewById(R.id.left);
button_right=(Button)findViewById(R.id.right);
web=(WebView) findViewById(R.id.web);
button=(Button) findViewById(R.id.button);
edit=(EditText) findViewById(R.id.edit);
button.setOnClickListener(this);
button_right.setOnClickListener(this);
button_left.setOnClickListener(this);
web.getSettings().setJavaScriptEnabled(true);
//web.goForward();
web.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
edit.setText(url);
//return super.shouldOverrideUrlLoading(view, url);
return true;
}
});
web.loadUrl("http://www.baidu.com");
}
@Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button:
web.loadUrl(String.valueOf(edit.getText()));
break;
case R.id.right:
web.goForward();//退回后一个网页
break;
case R.id.left:
web.goBack();//退回上一个网页
break;
}
}
}