常用UI控件
public class DemoActivity extends Activity {
AlertDialog dialog;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@Override
protected void onStop() {
System.out.println("stop");
super.onStop();
}
@Override
protected void onPause() {
System.out.println("pause");
super.onPause();
}
/*
通知对话框
Dialog是属于某个Activity的.当点击Dialog的时候不会触发Activity的OnPause()方法.
Dialog是在某个Activity上的.传递上下文的时候一定要用this.用getApplicationContext会出错.
*/
public void click1(View view) {
// 1.需要一个对话框的创建器
// getApplicationContext() 和 DemoActivity.this
AlertDialog.Builder buidler = new Builder(DemoActivity.this);
buidler.setTitle("title");
buidler.setMessage("message");
buidler.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
System.out.println("确定");
}
});
buidler.setNegativeButton("取消", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
System.out.println("取消 ");
}
});
AlertDialog dialog = buidler.create();
dialog.show();
}
//列表对话框
public void click2(View view) {
final String[] items = { "Red", "Green", "Blue" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
//列表单选对话框
public void click3(View view) {
final CharSequence[] items = { "Red", "Green", "Blue" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
// If -1 no items are checked.
builder.setSingleChoiceItems(items, -1,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
alert.show();
}
//列表多选对话框
public void click4(View view) {
final String[] items = { "java", ".net", "php" };
new AlertDialog.Builder(this)
.setCancelable(false)
.setTitle("选择语言")
.setMultiChoiceItems(items,
new boolean[] { false, true, false },
new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which, boolean isChecked) {
if (isChecked) {
Toast.makeText(getApplicationContext(),
items[which], Toast.LENGTH_SHORT)
.show();
}
}
})
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialoginterface, int i) {
dialoginterface.dismiss();
}
}).show();// 显示对话框
}
// 自定义对话框
public void click5(View view) {
LayoutInflater inflater = (LayoutInflater) this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogview = inflater.inflate(R.layout.mydialog, null);
Button butoon1 = (Button) dialogview.findViewById(R.id.button1);
butoon1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
System.out.println("button1 被点击");
}
});
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("自定义的对话框");
builder.setView(dialogview);
dialog = builder.create();
dialog.show();
}
//进度条对话框
public void click6(View view) {
ProgressDialog pd = new ProgressDialog(this);
pd.setTitle("正在提交");
pd.setMessage("提交中...");
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// pd.setProgressStyle(android.R.style.Widget_ProgressBar_Horizontal);
// pd.setIndeterminate(false);
pd.setMax(200);
pd.show();
}
//spinner
public void click7(View view) {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("java");
adapter.add("dotNet");
adapter.add("php");
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Spinner spinner = (Spinner) adapterView;
String itemContent = (String) adapterView
.getItemAtPosition(position);
System.out.println(itemContent);
}
@Override
public void onNothingSelected(AdapterView<?> view) {
Log.i("DemoActivity", view.getClass().getName());
}
});
}
}
//Seekbar进度条
public class SeekbarActivity extends Activity {
private SeekBar seekBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main
);
//AutoCompleteTextView 自动提示
/*
首先在布局文件中声明一个AutoCompleteTextView控件
<AutoCompleteTextView
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:completionThreshold="1"
android:id="@+id/name" />
*/
String[] names = {"老张", "老方", "老毕", "李明" , "李丽", "陈江", "abc", "acc"};
AutoCompleteTextView nameText = (AutoCompleteTextView)this.findViewById(R.id.name);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, names);
nameText.setAdapter(adapter);
seekBar = (SeekBar) findViewById(R.id.seekBar);
seekBar.setMax(100);// 设置最大刻度
seekBar.setProgress(30);// 设置当前刻度
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromTouch) {
Log.v("onProgressChanged()", String.valueOf(progress) + ", "
+ String.valueOf(fromTouch));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {// 开始拖动
Log.v("onStartTrackingTouch()",
String.valueOf(seekBar.getProgress()));
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {// 结束拖动
Log.v("onStopTrackingTouch()",
String.valueOf(seekBar.getProgress()));
}
});
Button button = (Button) this.findViewById(R.id.seekBarButton);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(SeekbarActivity.this,
String.valueOf(seekBar.getProgress()), 1).show();
}
});
}
}