android里的Spinner其实就是个ComboBox =。=
一。基本使用方法:
main.xml 不多解释
< LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android"
android:orientation ="vertical"
android:layout_width ="fill_parent"
android:layout_height ="fill_parent"
>
< Spinner
android:id = "@+id/mySpinner"
android:layout_width = "fill_parent"
android:layout_height ="wrap_content"
/>
</ LinearLayout >
在string.xml中使用“string-array”定义数据源。
< resources >
< string name ="app_name" > MySpinnerDemo </ string >
< string-array name = "phones_array" >
< item > iPhone </ item >
< item > Android </ item >
< item > BlackBerry </ item >
</ string-array >
</ resources >
Activity类:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
/**
* Spinner Demo
* @author Yinger
* @time 2011-7-9 下午01:54:59
* @mail melody.crazycoding@gmail.com
*/
public class SpinnerDemo extends Activity {
Spinner spinner = null ;
String selected = " 0 " ;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner = (Spinner)findViewById(R.id.mySpinner);
initMySpinner();
}
private void initMySpinner() {
ArrayAdapter < CharSequence > adapter = ArrayAdapter.createFromResource(
this , R.array.phones_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setPrompt( " test " );
spinner.setOnItemSelectedListener( new SpinnerOnSelectedListener());
}
class SpinnerOnSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView <?> adapterView, View view, int position,
long id) {
// TODO Auto-generated method stub
selected = adapterView.getItemAtPosition(position).toString();
System.out.println( " selected===========> " + selected);
}
public void onNothingSelected(AdapterView <?> arg0) {
// TODO Auto-generated method stub
System.out.println( " selected===========> " + " Nothing " );
}
}
}
二。debug发现的一个小问题:
Spinner在初始化时会自动调用一次OnItemSelectedListener事件
原因:有人说是Bug,其实这与C#的事件机制类似,懒得说了=。=
提供的解决办法:
个人是通过在事件注册之前调用
但要注意,使用此方法,如果用户选择的也是第一项,那么OnItemSelectedListener事件不会被触发……
三。使用技巧
1.动态添加Spinner的数据源
修改initMySpinner方法,代码如下:
String[] phones = { " iPhone " , " Android " , " BlackBerry " };
ArrayAdapter < String > adapter = new ArrayAdapter < String > (
this , android.R.layout. simple_spinner_item,
phones);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setPrompt( " test " );
spinner.setSelection( 0 , true );
spinner.setOnItemSelectedListener( new SpinnerOnSelectedListener());
}
2.自定义Spinner的Layout,替换掉体统默认的android.R.layout.simple_spinner_item
super
easy
my_spinner_item.xml:
< TextView xmlns:android ="http://schemas.android.com/apk/res/android"
android:layout_width ="fill_parent"
android:layout_height ="wrap_content"
android:textSize ="12dip"
android:textColor ="#FF8B1500"
android:gravity ="center" />
修改adapter:
this , R.layout.my_spinner_item,
phones);
同理,可自定义layout,替换掉android.R.layout.simple_spinner_dropdown_item
3.同时显示图片和文本
自定义Adapter:
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* 自定义Adapter
* @author Yinger
* @time 2011-7-9 下午03:39:34
* @mail melody.crazycoding@gmail.com
*/
public class MyAdapter extends BaseAdapter {
private Context ctx;
private int drawableIDs[];
private int stringIDs[];
public MyAdapter(Context ctx, int DrawableIDs[], int StringIDs[])
{
this .ctx = ctx;
this .drawableIDs = DrawableIDs;
this .stringIDs = StringIDs;
}
public int getCount() {
// TODO Auto-generated method stub
return drawableIDs.length ;
}
public Object getItem( int position) {
// TODO Auto-generated method stub
return drawableIDs [position];
}
public long getItemId( int position) {
// TODO Auto-generated method stub
return position;
}
public View getView( int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout( ctx );
ll.setOrientation(LinearLayout. HORIZONTAL );
ll.setGravity(Gravity. CENTER_VERTICAL );
ImageView iv = new ImageView( ctx );
iv.setImageResource( drawableIDs [position]);
iv.setLayoutParams( new ViewGroup.LayoutParams( 100 , 40 ));
ll.addView(iv);
TextView tv = new TextView( ctx );
tv.setText( stringIDs [position]);
tv.setTextSize( 14 );
tv.setTextColor(Color.BLUE );
ll.addView(tv);
return ll;
}
}
修改initMySpinner方法:
int [] phonePics = {R.drawable.apple,R.drawable.android,R.drawable.blackberry};
int [] phones = { R.string.iphone, R.string.android, R.string.blackberry};
MyAdapter adapter = new MyAdapter( this ,phonePics,phones);
spinner.setAdapter(adapter);
spinner.setPrompt( " test " );
spinner.setSelection( 0 , true );
}
运行结果截图:
OK,但使用自定义的Adapter,我们如何来获取选中的文本信息呢?
在MyAdapter中,修改getView方法,添加黄色区域代码如下:
// TODO Auto-generated method stub
LinearLayout ll = new LinearLayout( ctx );
ll.setOrientation(LinearLayout. HORIZONTAL );
ll.setGravity(Gravity. CENTER_VERTICAL );
ImageView iv = new ImageView( ctx );
iv.setImageResource( drawableIDs [position]);
iv.setLayoutParams( new ViewGroup.LayoutParams( 100 , 40 ));
ll.addView(iv);
TextView tv = new TextView( ctx );
tv.setText( stringIDs [position]);
tv.setTextSize( 14 );
tv.setTextColor(Color.BLUE );
tv.setTag( " tagTextView " );
ll.addView(tv);
return ll;
}
重写事件中的onItemSelected方法:
public void onItemSelected(AdapterView <?> adapterView, View view, int position,
long id) {
if (adapterView.getId() == R.id.mySpinner)
{
LinearLayout ll = (LinearLayout)view;
TextView tv = (TextView)ll.findViewWithTag( " tagTextView " );
String str = (String)tv.getText();
System.out.println( " selected===========> " + str);
}
}
最后,别忘了注册该事件。=。=
转自:http://www.blogjava.net/crazycoding/archive/2011/07/09/353981.html
android 下如果做处理图片的软件 可以调用系统的控件 实现缩放切割图片 非常好的效果
import java.io.ByteArrayOutputStream; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; public class testActivity extends Activity { public static final int NONE = 0; public static final int PHOTOHRAPH = 1;// 拍照 public static final int PHOTOZOOM = 2; // 缩放 public static final int PHOTORESOULT = 3;// 结果 public static final String IMAGE_UNSPECIFIED = "image/*"; ImageView imageView = null; Button button0 = null; Button button1 = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imageView = (ImageView) findViewById(R.id.imageID); button0 = (Button) findViewById(R.id.btn_01); button1 = (Button) findViewById(R.id.btn_02); button0.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); startActivityForResult(intent, PHOTOZOOM); } }); button1.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "temp.jpg"))); startActivityForResult(intent, PHOTOHRAPH); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == NONE) return; // 拍照 if (requestCode == PHOTOHRAPH) { //设置文件保存路径这里放在跟目录下 File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg"); startPhotoZoom(Uri.fromFile(picture)); } if (data == null) return; // 读取相册缩放图片 if (requestCode == PHOTOZOOM) { startPhotoZoom(data.getData()); } // 处理结果 if (requestCode == PHOTORESOULT) { Bundle extras = data.getExtras(); if (extras != null) { Bitmap photo = extras.getParcelable("data"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)压缩文件 imageView.setImageBitmap(photo); } } super.onActivityResult(requestCode, resultCode, data); } public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, IMAGE_UNSPECIFIED); intent.putExtra("crop", "true"); // aspectX aspectY 是宽高的比例 intent.putExtra("aspectX", 1); intent.putExtra("aspectY", 1); // outputX outputY 是裁剪图片宽高 intent.putExtra("outputX", 64); intent.putExtra("outputY", 64); intent.putExtra("return-data", true); startActivityForResult(intent, PHOTORESOULT); } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <ImageView android:id="@+id/imageID" android:adjustViewBounds="true" android:maxWidth="50dip" android:maxHeight="50dip" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_01" android:layout_height="50dip" android:text="相册" android:layout_width="150dip"/> <Button android:id="@+id/btn_02" android:layout_height="50dip" android:text="拍照" android:layout_width="150dip"/> </LinearLayout>
使用listView或者gridView时,当列表为空时,有时需要显示一个特殊的empty view来提示用户,一般情况下,如果你是继承ListActivity,只要
<ListView android:id="@id/android:list".../>
<TextView android:id="@id/android:empty.../>
当列表为空时就会自动显示TextView
但是,如果继承Activity的话,想出现上面的效果,就需要手动
<ListView android:id="@+id/list" .../>
<TextView android:id="@+id/empty" .../>
ListView list= (ListView)findViewById(R.id.mylist);
TextView tv= (TextView)findViewById(R.id.myempty);
list.setEmptyView(tv);
误区:
setEmptyView(View)这个函数很有误导性,有时可能会在代码中写EmptyView,像下面这样:
TextView tv= new TextView(this);
tv.setText("this is a empty view")
setEmptyView(tv);
这样是不行的。。。
但是后来我在老外的网上说下面这样是可行的,注意第4,5行:
TextView emptyView = new TextView(context); emptyView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); emptyView.setText(“This appears when the list is empty”); emptyView.setVisibility(View.GONE); ((ViewGroup)list.getParent()).addView(emptyView); list.setEmptyView(emptyView);
结果真的可行!
转自:http://gundumw100.iteye.com/blog/1165673