
TextView tv = new TextView(this);
SpannableStringBuilder style=new SpannableStringBuilder("这是测试111");
style.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.YELLOW), 2, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
style.setSpan(new ForegroundColorSpan(Color.BLUE), 4, 6, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(style);ImageButton
- red
- green
- blue
final Spinner sp = (Spinner)this.findViewById(R.id.spinner); //准备数据 ArrayAdapteradapter = ArrayAdapter.createFromResource( this, R.array.colors, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); //把数据设置到spinner sp.setAdapter(adapter);
ArrayAdapteradapter = new ArrayAdapter (this,android.R.layout.simple_spinner_item); adapter.add("red"); adapter.add("blue");
sp.getSelectedItem().toString()
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return new DatePickerDialog(this,
mDateSetListener,
mYear, mMonth, mDay);
}
return null;
}protected Dialog onCreateDialog(int id) {
switch (id) {
case TIME_DIALOG_ID:
return new TimePickerDialog(this,
mTimeSetListener, mHour, mMinute, false);
}
return null;
}class ImageAdapter extends BaseAdapter {
private Context mContext;
private Integer[] mImageIds = {R.drawable.icon,R.drawable.icon,
R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon,
R.drawable.icon,R.drawable.icon,R.drawable.icon,R.drawable.icon};
public ImageAdapter(Context c) { mContext = c; }
public int getCount() { return mImageIds.length; }
public Object getItem(int position) { return position; }
public long getItemId(int position) { return position; }
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
}
}
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.MyGallery);
mGalleryItemBackground = attr.getResourceId(
R.styleable.MyGallery_android_galleryItemBackground, 0);imageView.setBackgroundResource(mGalleryItemBackground);
AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.autocomplete); ArrayAdapteradapter = new ArrayAdapter (this, R.layout.autoc_item, new String[]{"abc","abd","abcd","bcd","bef"}); textView.setAdapter(adapter);
MultiAutoCompleteTextView textView = (MultiAutoCompleteTextView) findViewById(R.id.autocomplete); ArrayAdapteradapter = new ArrayAdapter (this, R.layout.autoc_item, new String[]{"abc","abd","abcd","bcd","bef"}); textView.setAdapter(adapter); textView.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
GridView gridview = (GridView) findViewById(R.id.gridview); gridview.setAdapter(new ImageAdapter(this));
class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) { mContext = c; }
public int getCount() {return mThumbIds.length; }
public Object getItem(int position) { return null;}
public long getItemId(int position) { return 0; }
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setPadding(0, 0,0,0);
imageView.setBackgroundColor(Color.RED);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon,
R.drawable.icon, R.drawable.icon
};
}gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v,
int position, long id) {
Toast.makeText(TestWidgets.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});nTabHost就是用来存放多个Tab的容器
TabHost tabHost = getTabHost();
Intent intent = new Intent().setClass(this, MyActivity.class);
intent.putExtra("showStr", "第一个tab");
Intent设置的Class就是点击tab,内容页显示出来的Activity。tabHost.addTab(spec);
Resources res = getResources();
spec = tabHost.newTabSpec("FirstTag").setIndicator(
"第一个Tab",res.getDrawable(R.drawable.my_tab1_selector))
.setContent(intent);public class TestList extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// this.setContentView(R.layout.TestList);
setListAdapter(new ArrayAdapter(this,
android.R.layout.simple_list_item_1, new String[]{"111","222","333"}));
}
protected void onListItemClick(ListView l,View v,int position,long id){
super.onListItemClick(l, v, position, id);
String s = ""+l.getItemAtPosition(position);
System.out.println("now click="+s);
}
}
setListAdapter(new ArrayAdapter(this, android.R.layout. simple_list_item_single_choice, new String[]{"111","222","333})); this.getListView().setItemsCanFocus(false); this.getListView().setChoiceMode(ListView. CHOICE_MODE_SINGLE);
setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, new String[]{"111","222","333})); this.getListView().setItemsCanFocus(false); this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
List list = new ArrayList();
Map map1 = new HashMap();
map1.put("userId","11");
map1.put("userName","11N");
map1.put("age","11");
list.add(map1);
Map map2 = new HashMap();
map2.put("userId","22");
map2.put("userName","22N");
map2.put("age","22");
list.add(map2);
setListAdapter(new SimpleAdapter(this, list,R.layout.list,new String[]{"userId","userName","age"}, new int[]{R.id.userId,R.id.userName,R.id.userAge})); protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
System.out.println("sssssssss===="+l.getItemAtPosition(position));public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
return true;
}public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.to_add:
Log.i("javass","now to add"); break;
case R.id.to_update:
Log.i("javass","now to update"); break;
default: return super.onOptionsItemSelected(item);
}
return true;
}public void myToAdd(MenuItem item) {
Log.i("javass","now in myToAdd");
}this.registerForContextMenu(btnRun);
public void onCreateContextMenu(ContextMenu menu,View v,ContextMenuInfo info) {
super.onCreateContextMenu(menu, v, info);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.test_menu, menu);
}public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id. new_file:
Log.i("onContextItemSelected", "now ruuuuuuuuuuu");
return true;
default: return super.onContextItemSelected(item);
}
}public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
MenuItem mi = menu.findItem(R.id.create_new);
mi.setChecked(flag);
return true;
}ActionBar actionBar = getActionBar(); actionBar.hide();
或者把android:actionLayout=“@layout/searchview” 换成: android:actionViewClass="android.widget.SearchView"
ActionBar actionBar = getActionBar(); actionBar.setDisplayUseLogoEnabled(true); actionBar.setLogo(R.drawable.app_sample_code); actionBar.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.app_sample_code));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("确定要删除吗?")
.setCancelable(false)
.setPositiveButton("取消",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
Toast.makeText(HelloWorldActivity.this, “dialog取消", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) { Toast.makeText(HelloWorldActivity.this,“dialog确定", Toast.LENGTH_LONG).show();
}
});
AlertDialog alert = builder.create();final CharSequence[] items = { "红色", "绿色", "蓝色" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择颜色");
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();final CharSequence[] items = { "红色", "绿色", "蓝色" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择颜色");
builder.setSingleChoiceItems(items, -1,new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText(getApplicationContext(), items[item],
Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog alert = builder.create();inal CharSequence[] items = { "红色", "绿色", "蓝色" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("请选择颜色");
builder.setMultiChoiceItems(items, new boolean[]{false,false,false},new DialogInterface.OnMultiChoiceClickListener() {
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
Toast.makeText(getApplicationContext(), items[which], Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("确定",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Toast.makeText(HelloWorldActivity.this, "dialog确定", Toast.LENGTH_LONG).show();
dialog.dismiss();
}
}
);
AlertDialog alert = builder.create();
HandlerThread ht = new HandlerThread("MyThread");
Handler h = null;if(!ht.isAlive()){
ht.start();
h = new Handler(ht.getLooper()){
public void handleMessage(Message msg) {
try {
Thread.sleep(5000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
HelloWorldActivity.this.dismissDialog(1);
super.handleMessage(msg);
}
};
}
Message msg = h.obtainMessage();
msg.sendToTarget();dialog = new ProgressDialog(HelloWorldActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("装载中...");
dialog.setCancelable(false);
dialog.setMax(10);Handler h = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.arg1 < dialog.getMax()){dialog.setProgress(msg.arg1);}
else{dialog.dismiss();}}};
int num = 0;
Runnable r = new Runnable() {
public void run() {
Message msg = h.obtainMessage();
msg.arg1 = num++;
h.sendMessage(msg);
h.postDelayed(r, 1000L);
}};pb = (ProgressBar)HelloWorldActivity.this.findViewById(R.id.progress_bar); pb.setMax(10); pb.setVisibility(View.VISIBLE); h.post(r);
Context mContext = HelloWorldActivity.this;
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.my_dialog);
dialog.setTitle("自己订制的Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("欢迎使用本系统");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
//最后显示出来
dialog.show();

Handler h = new Handler() {
public void handleMessage(Message msg) {
super.handleMessage(msg);
//使用what来区分是什么样的消息
switch (msg.what){
case 1 :
//这里进行消息的处理
break;
default :
//这里进行消息的处理
}
}};
int what = 0;
Runnable r = new Runnable() {
public void run() {
Message msg = h.obtainMessage();
msg.obj = "1234567";//传递的参数
msg.what = what;
what++;
h.sendMessage(msg);
h.postDelayed(r, 1000L);
}};HandlerThread ht = new HandlerThread("MyThread");
Handler h = null;ht.start();
h = new Handler(ht.getLooper()){
public void handleMessage(Message msg) {
super.handleMessage(msg);
//这里真正处理消息
Toast.makeText(getApplicationContext(),"msg="+msg.obj,Toast.LENGTH_LONG).show();
}
};Message msg = h.obtainMessage(); msg.obj="test message"; msg.sendToTarget();
Toast t = Toast.makeText(HelloWorldActivity.this,"okok", Toast.LENGTH_SHORT); t.setGravity(Gravity.TOP|Gravity.LEFT, 5,10); t.show();
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.my_dialog,
(ViewGroup) findViewById(R.id.layout_root));
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("欢迎您的到来!");
Toast t = new Toast(HelloWorldActivity.this);
t.setView(layout);
t.setGravity(Gravity.CENTER|Gravity.LEFT, 5,10);
t.show();
//获取NotificationManager String ns = Context.NOTIFICATION_SERVICE; NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); //设置在status bar上显示的信息条 int icon = R.drawable.app_sample_code; CharSequence tickerText = "请注意"; long when = System.currentTimeMillis(); Notification notification = new Notification(icon, tickerText, when); //设置在status 列表里面显示的信息,以及点击的事件 CharSequence contentTitle = "重要通知"; CharSequence contentText = "测试"; Intent notificationIntent = new Intent(HelloWorldActivity.this, T2.class); PendingIntent contentIntent = PendingIntent.getActivity(HelloWorldActivity.this, 0, notificationIntent, 0); notification.setLatestEventInfo(HelloWorldActivity.this, contentTitle, contentText, contentIntent); //发出通知 mNotificationManager.notify(1, notification);
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26715458/viewspace-717426/ ,如需转载,请注明出处,否则将追究法律责任。