要想即时捕捉EditText中输入的内容,只要给EditText注册一个OnKeyListener(单击键盘事件处理)监听就可以了,在该监听事件里可以进行文字过滤。大部分widget都有setOnKeyListener方法没什么难点,只是在前面例子上稍作修改
package com.kevin.checkbox; import android.app.Activity; import android.os.Bundle; import android.text.method.HideReturnsTransformationMethod; import android.text.method.KeyListener; import android.text.method.PasswordTransformationMethod; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.TextView; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.EditText; public class Main extends Activity { private CheckBox chk_show; private EditText et_password; private TextView tv_result; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_password = (EditText) findViewById(R.id.et_password); chk_show = (CheckBox) findViewById(R.id.chk_show); tv_result = (TextView) findViewById(R.id.tv_password); chk_show.setOnCheckedChangeListener(new CheckChangedListener()); et_password.setOnKeyListener(new PasswordOnKeyListener()); } // Checkbox的选择监听事件 class CheckChangedListener implements OnCheckedChangeListener{ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(isChecked){ // 设置EditText的内容为显示 et_password.setTransformationMethod(HideReturnsTransformationMethod.getInstance()); }else{ // 设置EditText的内容为隐藏 et_password.setTransformationMethod(PasswordTransformationMethod.getInstance()); } } } // EditText的键盘输入事件 class PasswordOnKeyListener implements OnKeyListener{ @Override public boolean onKey(View v, int keyCode, KeyEvent event) { EditText password = (EditText) v; // 在textview显示键盘输入值 tv_result.setText(getString(R.string.result) + password.getText().toString()); return false; } } }
In an earlier post I talked about how to launch the browser from within an iPhone application using the UIApplication:openURL: method.
It is also possible to use this same technique to launch other applications on the iPhone that are very useful.
Examples of some of the key applications that you can launch via URL are:
- Launch Google Maps
- Launch Apple Mail
- Dial a Phone Number
- Launch the SMS Application
- Launch the Browser
- Launch the AppStore
Launch Google Maps
The URL string for launching Google Maps with a particular keyword follows this structure:
http://maps.google.com/maps?q=${QUERY_STRING}
The only trick to this is to ensure that the value for the ${QUERY_STRING} is properly URL encoded. Here is a quick example of how you would launch Google Maps for a specific address:
1 2 3 4 5 6 7 8 9 10 11
// Create your query ... NSString * searchQuery = @ "1 Infinite Loop, Cupertino, CA 95014" ; // Be careful to always URL encode things like spaces and other symbols that aren't URL friendly searchQuery = [ addressText stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding] ; // Now create the URL string ... NSString * urlString = [ NSString stringWithFormat: @ "http://maps.google.com/maps?q=%@" , searchQuery] ; // An the final magic ... openURL! [ [ UIApplication sharedApplication] openURL: [ NSURL URLWithString: urlText] ] ;
Also very useful, is the ability to enable a user to quickly send an email by launching the email client in compose mode and the address already filled out. The format of this URI should be familiar to anyone that has done any work with HTML and looks like this:
mailto://${EMAIL_ADDRESS}
For example, here we are opening the email application and filling the “to:” address with info@iphonedevelopertips.com :
[ [ UIApplication sharedApplication] openURL: [ NSURL URLWithString: @ "mailto://info@iphonedevelopertips.com" ] ] ;
You can use openURL: to dial a phone number. One advantage this has over other URLs that launch applications, is that the dialer will return control back to the application when the user hits the “End Call” button.
Anyone familiar with J2ME or WML will find this URL scheme familiar:
tel://${PHONE_NUMBER}
Here is an example of how we would dial the number (800) 867-5309:
1
[ [ UIApplication sharedApplication] openURL: [ NSURL URLWithString: @ "tel://8004664411" ] ] ;
NOTE When providing an international number you will need to include the country code.
Launch the SMS ApplicationAlso not supported by the iPod Touch, is the ability to quickly setup the SMS client so that your users can quickly send a text message. It is also possible to provide the body of the text message.
The format looks like this:
sms:${PHONENUMBER_OR_SHORTCODE}
NOTE: Unlike other URLs, an SMS url doesn’t use the “//” syntax. If you add these it will assume it is part of the phone number which is not.
1
[ [ UIApplication sharedApplication] openURL: [ NSURL URLWithString: @ "sms:55555" ] ] ;
NOTE: According to the official SMS specification, you should be able to send a body as well as the phone number by including “?body=” parameter on the end of the URL … unfortunately Apple doesn’t seem to support this standard.
Lauching Browser
Here is a simple example of how to open safari with a specific URL:
1 2
NSURL * url = [ NSURL URLWithString: @ "http://www.iphonedevelopertips.com" ] ; [ [ UIApplication sharedApplication] openURL: url] ;Launching the AppStore
Finally, it is worth noting that you can launch the AppStore and have the "buy" page of a specific application appear. To do this, there is no special URL scheme. All you need to do is open up iTunes to the application you want to launch; right-click on the application icon at the top left of the page; and select Copy iTunes Store URL .
The URL will look something like this:
http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8
Launching the AppStore URL is exactly the same as you would launch the browser. Using the link above, here is an example of how we would launch the AppStore:
1 2
NSURL * appStoreUrl = [ NSURL URLWithString: @ "http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id=291586600&mt=8" ] ; [ [ UIApplication sharedApplication] openURL: appStoreUrl] ;
FileBrowser.java
public class FileBrowser { private Context context; private List<String> items=null; private List<String> paths=null; private String rootPath="/"; private TextView mPath; private View view; private ListView list; private FileAdapter m_FileAdapter; private String selectedFilePath = ""; public String getSelectedFilePath() { return selectedFilePath; } public void setSelectedFilePath(String selectedFilePath) { this.selectedFilePath = selectedFilePath; } public View getFileListView(){ return view; } public FileBrowser(Context context){ this.context = context; LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.filelist, null); mPath = (TextView)view.findViewById(R.id.mPath); mPath.setTextColor(context.getResources().getColor(R.color.text_color)); list = (ListView)view.findViewById(R.id.filelist); getFileDir(rootPath); } private void getFileDir(String filePath){ mPath.setText(filePath); items=new ArrayList<String>(); paths=new ArrayList<String>(); File f=new File(filePath); File[] files=f.listFiles(); if(!filePath.equals(rootPath)) { items.add("goroot"); paths.add(rootPath); items.add("goparent"); paths.add(f.getParent()); } for(int i=0;i<files.length;i++) { File file=files[i]; items.add(file.getName()); paths.add(file.getPath()); } m_FileAdapter = new FileAdapter(context,items,paths); list.setAdapter(m_FileAdapter); list.setOnItemClickListener(new OnItemClickListener(){ @Override public void onItemClick(AdapterView<?> parent, View view,int position, long id) { File file=new File(paths.get(position)); if(file.canRead()){ if (file.isDirectory()){ getFileDir(paths.get(position)); }else{ ViewHolder vHollder = (ViewHolder) view.getTag(); for(ViewHolder v : m_FileAdapter.getVHs()){ v.cBox.setChecked(false); v.cBox.setVisibility(View.GONE); } for(int i : m_FileAdapter.getIsSelected().keySet()){ m_FileAdapter.getIsSelected().put(i, false); } for(int i : m_FileAdapter.getIsVisibility().keySet()){ m_FileAdapter.getIsVisibility().put(i, View.GONE); } vHollder.cBox.setVisibility(View.VISIBLE); m_FileAdapter.getIsVisibility().put(position, View.VISIBLE); vHollder.cBox.setChecked(true); m_FileAdapter.getIsSelected().put(position, vHollder.cBox.isChecked()); if(mPath.getText().toString().equals(rootPath)){ setSelectedFilePath(mPath.getText().toString() + vHollder.text.getText().toString()); }else{ setSelectedFilePath(mPath.getText().toString() + File.separator + vHollder.text.getText().toString()); } Toast.makeText(context, getSelectedFilePath(), Toast.LENGTH_SHORT).show(); } }else{ LinearLayout lay = new LinearLayout(context); lay.setOrientation(LinearLayout.HORIZONTAL); ImageView image = new ImageView(context); TextView text = new TextView(context); text.setTextColor(context.getResources().getColor(R.color.text_color)); text.setTextSize(16); text.setText("很抱歉您的权限不足,无法读取文件内容!"); Toast toast = Toast.makeText(context, text.getText().toString(), Toast.LENGTH_SHORT); image.setImageResource(android.R.drawable.ic_lock_idle_alarm); lay.addView(image); lay.addView(text); toast.setView(lay); toast.show(); } } }); } }
FileAdapter.java
public class FileAdapter extends BaseAdapter{ private LayoutInflater mInflater; private Bitmap mIcon1; private Bitmap mIcon2; private Bitmap mIcon3; private Bitmap mIcon4; private List<String> items; private List<String> paths; private Map<Integer, Boolean> isSelected; private Map<Integer, Integer> isVisibility; public Map<Integer, Integer> getIsVisibility() { return isVisibility; } private List<ViewHolder> VHs = new ArrayList<ViewHolder>(); private Context context; public Map<Integer, Boolean> getIsSelected() { return isSelected; } public void setIsSelected(Map<Integer, Boolean> isSelected) { this.isSelected = isSelected; } public FileAdapter(Context context,List<String> it,List<String> pa){ this.context = context; mInflater = LayoutInflater.from(context); if( it != null && it.size() > 0){ isSelected = new HashMap<Integer, Boolean>(); isVisibility = new HashMap<Integer, Integer>(); for (int i = 0; i < it.size(); i++) { isSelected.put(i, false); isVisibility.put(i, View.GONE); } } items = it; paths = pa; mIcon1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.backroot); mIcon2 = BitmapFactory.decodeResource(context.getResources(), R.drawable.backparent); mIcon3 = BitmapFactory.decodeResource(context.getResources(), R.drawable.folder); mIcon4 = BitmapFactory.decodeResource(context.getResources(), R.drawable.doc); } @Override public int getCount(){ return items.size(); } @Override public Object getItem(int position){ return items.get(position); } @Override public long getItemId(int position){ return position; } @Override public View getView(int position,View convertView,ViewGroup parent){ ViewHolder holder; if(convertView == null){ convertView = mInflater.inflate(R.layout.file_row, null); holder = new ViewHolder(); VHs.add(holder); holder.text = (TextView) convertView.findViewById(R.id.filetext); holder.text.setTextColor(context.getResources().getColor(R.color.text_color)); holder.icon = (ImageView) convertView.findViewById(R.id.fileicon); holder.cBox = (CheckBox) convertView.findViewById(R.id.file_check); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); } File f=new File(paths.get(position).toString()); if(items.get(position).toString().equals("goroot")){ holder.text.setText("返回根目录"); holder.icon.setImageBitmap(mIcon1); holder.cBox.setChecked(isSelected.get(position)); holder.cBox.setVisibility(isVisibility.get(position)); }else if(items.get(position).toString().equals("goparent")){ holder.text.setText("返回上一级"); holder.icon.setImageBitmap(mIcon2); holder.cBox.setChecked(isSelected.get(position)); holder.cBox.setVisibility(isVisibility.get(position)); }else{ holder.text.setText(f.getName()); if(f.isDirectory()){ holder.icon.setImageBitmap(mIcon3); holder.cBox.setChecked(isSelected.get(position)); holder.cBox.setVisibility(isVisibility.get(position)); }else{ holder.icon.setImageBitmap(mIcon4); holder.cBox.setChecked(isSelected.get(position)); holder.cBox.setVisibility(isVisibility.get(position)); } } return convertView; } public List<ViewHolder> getVHs() { return VHs; } public final class ViewHolder { public TextView text; public ImageView icon; public CheckBox cBox; } }
file_row.xml(layout)
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/file_items" android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content" android:paddingBottom="4dip" android:paddingLeft="12dip" android:paddingRight="12dip"> <ImageView android:id="@+id/fileicon" android:layout_height="wrap_content" android:textSize="26dip" android:layout_alignParentLeft="true" android:layout_width="wrap_content" android:layout_below="@+id/file_items"/> <TextView android:id="@+id/filetext" android:layout_gravity="center_vertical" android:layout_width="fill_parent" android:layout_toRightOf="@id/fileicon" android:paddingLeft="20dip" android:layout_height="wrap_content"/> <CheckBox android:id="@+id/file_check" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:focusable="false" android:focusableInTouchMode="false" android:clickable="false" android:layout_below="@+id/file_items"/> </RelativeLayout>