我們舉個例子,假如我們已經獲得了手機中保存的聯繫人姓名和電話號碼,並把它們顯示在了一個Android ListView中,現在要實現的功能是當點擊選中項時直接撥號,那麼如何取得此時Android ListView中的號碼?
要顯示聯繫人姓名和電話號碼,那你現在肯定已經在listview 的item裏面放了兩個控件吧,假如是textview吧,那你就首先要給listview添加一個OnItemClickListener來監聽你點擊了那個item,在這個OnItemClickListener裏面有個onItemClick方法,這個方法裏面有個view參數,那你就可以通過view.findViewById("你的控件名字")來找到了。代碼例子:
- ListView listView =(ListView) findViewById(R.id.ListView);
- OnItemClickListener lvItemClickListener=new OnItemClickListener(){
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- final TextView content=(TextView) view.findViewById(R.id.phoneNumber);)
- }
- }
Android中如何使EditText在初始化時不得到焦點
如何使Android EditText在初始化時不得到焦點:
使用:
myEditText.clearFocus();
myEditText.setSelected(false);
都沒好用,正確的方法是在EditText上面加一個隱藏的linearLayout,如下:
- <LinearLayout
- android:focusable="true"
- android:focusableInTouchMode="true"
- android:layout_width="0px"
- android:layout_height="0px"
- />
- <EditText
- android:id="@+id/editor"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:paddingRight="55dip"
- android:background="@drawable/register"
- android:editable="false"
- android:maxLines="1"
- android:scrollHorizontally="true"
- android:cursorVisible="false"
- />
Android中用Intent提取縮略圖和原始圖像提取圖像的Thumbnail:
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
- intent.setType("image/*");
- intent.putExtra("return-data", true);
- startActivityForResult(intent, REQUEST_CODE_PHOTO_PICKED);
-
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode != RESULT_OK) {
- return;
- }
- Bitmap bitmap = null;
- ContentResolver resolver = getContentResolver();
- if (requestCode == REQUEST_CODE_PHOTO_PICKED) {
- final Bundle extras = data.getExtras();
- if (extras != null)
- bitmap = extras.getParcelable("data");
- }
- }
- //此方法返回的data 必須小於100k
獲取原圖:
- Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
- intent.setType("image/*");
- startActivityForResult(intent, REQUEST_CODE_PHOTO_PICKED);
-
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode != RESULT_OK) {
- return;
- }
- Bitmap bitmap = null;
- ContentResolver resolver = getContentResolver();
- if (requestCode == REQUEST_CODE_PHOTO_PICKED) {
- try {
- Uri originalUri = data.getData();
- Uri thumb = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
- originalUri.getLastPathSegment());
- bitmap = MediaStore.Images.Media.getBitmap(resolver, thumb);
- } catch (IOException e) {
- }
- }
- }