Android控件知识

¥开启¥

【浏览器WebView】

〖2017-08-16 10:13:15〗

《1.设置支持JavaScript: €webView.getSettings().setJavaScriptEnabled(true); €2.加载网页: €webView.loadUrl("http://www.baidu.com"); €//加载本地文件: €webView.loadUrl("file:///storage/sdcard0/index.html"); €webView.loadUrl("content://com.android.htmlfileprovider/sdcard/index.html"); €//加载assets中文件: €webView.loadUrl("file:///android_asset/index.html"); €3.获取原始URL:webView.getOriginalUrl(); €4.获取当前URL:webView.getUrl(); €5.浏览器加载网页的监听 €webView.setWebViewClient(new WebViewClient() { €@Override €public void onPageFinished(WebView view,String url) { €//加载完成 €} €@Override €public€boolean€shouldOverrideUrlLoading(WebView view, String url) { €//设置在本浏览器打开新网页 €view.loadUrl(url); €return true;€} €}); €€7.设置webview背景透明:€webView.setBackgroundColor(0); €//4.0上在xml中关闭硬件加速€android:layerType="software" €€8.后退网页:€if (webView.canGoBack()) { €webView.goBack(); €} €€9.下载链接单击监听事件 :€webView.setDownloadListener(new DownloadListener{€@Override€public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype,long contentLength) {€€Uri uri = Uri.parse(url);  //调用本地浏览器打开€Intent intent = new Intent(Intent.ACTION_VIEW, uri);€startActivity(intent);€}  €} );€€10.保存浏览器网页截图:€Picture pic = webView.capturePicture(); €int width = pic.getWidth(); €int height = pic.getHeight(); €if (width > 0 && height > 0) { €Bitmap bmp=Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); €Canvas canvas=new Canvas(bmp); €pic.draw(canvas);€try { €String filename="sdcard0/"+System.currentTimeMillis()+".jpg"; €FileOutputStream fos=newFileOutputStream(filename); €if(fos!=null){€bmp.compress(Bitmap.CompressFormat.JPEG, 90, fos); €fos.close(); €} €} catch (Exception e) { €e.printStackTrace(); €} €} €》

『小菜』

〔Android控件知识〕

【日期和时间选择控件】

〖2017-08-16 10:17:56〗

《一、日期控件 DatePicker :€€//设置右边日历不显示 €android:calendarViewShown="false" €//设置默认时间和改变后的监听事件 €datePicker.init(2015,05,10,new DatePicker.OnDateChangedListener()€{ €€@Override €public void onDateChanged(DatePicker datePicker, int i, int i2, int i3) { €€ //后三个参数是改变后的年月里,月是0开始的 €} €}); €€二、时间控件 TimePicker :€€//设置为24小时制 €timePicker.setIs24HourView(true); €//时间改变后的监听 €timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() { €@Override €public void onTimeChanged(TimePicker timePicker, int i, int i2) { €€//后两个参数是控件选择的时,分 €} €}); €€三、时间和日期选择对话框:€//日期选择对话框: €DatePickerDialog datePickerDialog = new DatePickerDialog(this,CallBack,2015,05,10); €//时间选择对话框: €TimePickerDialog timePickerDialog = new TimePickerDialog(this, CallBack,12,12,true); € //后三个参数默认时间,时间最后参数是否24小时制€€2.CallBack选择完成后的事件:€€//日期事件 €DatePickerDialog.OnDateSetListener CallBack = new DatePickerDialog.OnDateSetListener() {€ @Override €public void onDateSet(DatePicker datePicker, int i, int i2, int i3) { €//后三个参数年,月,日 € } €}; €€//时间事件 €TimePickerDialog.OnTimeSetListener CallBack = new TimePickerDialog.OnTimeSetListener() { €@Override €public void onTimeSet(TimePicker timePicker, int i, int i2) { €//后两个参数,时,分 €} €}; €》

『小菜』

〔Android控件知识〕

【EditText编辑框】

〖2017-08-16 13:57〗

《一、xml文件中属性: €1.最大长度:android:maxLength="10"€ 2.默认内容:android:hint="请输入内容" €android€textColorHint="#000000" €3.限制输入:android:digits="123+" €4.单行显示:android:singleLine="true" €5.每行长度:android:ems="5" €6.最多几行:android:Lines="4" €€二、java判断编辑框是否为空: €//去空格 €String serch_text = editText.getText().toString().trim(); €if(serch_text.isEmpty()){    } €€三、编辑框内容改变时监听: €editText.addTextChangedListener(new TextWatcher() { €@Override €public void€beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) { € //正在修改 €} €@Override €public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) { €//修改之前€} €@Override €public void afterTextChanged(Editable editable) { €//修改之后的内容 String str = editable.toString(); €//清除输入内容 editable.clear();€} €}); €€四、添加过滤器: €€editText.setFilters(new InputFilter[]{null,null}); €//过滤器示例: €1.小写转换成大写 €new InputFilter.AllCaps()€€2.自定义过滤器 €new InputFilter()€{ €@Override €public CharSequence filter(CharSequence charSequence, int i, int i2, Spanned spanned, int i3, int i4) { €//charSequence正在输入的字符,spanned上次输入的字符 €//过滤非法字符替换成*返回就可以了€return charSequence ; €}€}€€五、设置editText能否编辑:€€1.不可编辑: €editText.setFocusable(false); €contentEdit.setFocusableInTouchMode(false);€€2.可编辑: €editText.setFocusableInTouchMode(true);€editText.setFocusable(true); €editText.requestFocus(); €》

『小菜』

〔Android控件知识〕

【EditText文本高亮】

〖2017-08-16 14:01〗

《/** € *HashMap存放图片的文字代码和图片的路径 €*/ €HashMap€keyImage = new HashMap(); €key.put("public",MainActivity.this.getResources().getColor(R.color.red)); €key.put("void",MainActivity.this.getResources().getColor(R.color.red)); €€TextView textView = (TextView) findViewById(R.id.content_editText1); €//为TextView添加文本改变监听 €textView.addTextChangedListener(new MarkImageWatcher(keyImage,MainActivity.this)); €textView.setText("[image01]"); €/** € *自定义TextWatcher类 € */ €public class MarkImageWatcher€implements€TextWatcher { €HashMap key = new HashMap();€Context context; €//构造方法€public€MarkImageWatcher(HashMap key, Context context) { €super();€this.key = key; €this.context = context; €}€@Override €public void€afterTextChanged(Editable arg0) { €String text = arg0.toString();€Iterator iterator = key.keySet().iterator(); €while (iterator.hasNext()) { €String k = iterator.next();€if (text.contains(k)) { €int index = 0; €while ((index = text.indexOf(k, index)) != -1) {€arg0.setSpan(new ForegroundColorSpan(key.get(k)), index,index + k.length(),Spanned SPAN_EXCLUSIVE_EXCLUSIVE); €index += k.length(); €} €}€}€} €} €与标记图片类似,TextView和EditText可以 €互换,EditText本身也是从TextView继承过来的。所

以EditText也可以标记图片,Textview也可以标记颜色》

『小菜』

〔Android控件知识〕

【菜单选项操作】

〖2017-08-16 17:21:10〗

《java代码写在Activity类体中 €€// 加载菜单布局 €@Override €public boolean onCreateOptionsMenu(Menu menu) { €getMenuInflater().inflate(R.menu.main, menu); €return true; €} €// 菜单点击事件€public€boolean€onOptionsItemSelected(MenuItem item) { €switch (item.getItemId()) { €case R.id.exit: €finish(); €break; €case R.id.about: €Toast.makeText(MainActivity.this, "你好",Toast.LENGTH_SHORT).show(); €break; €} €return super.onOptionsItemSelected(item);€} €//菜单布局文件目录位于res/menu下 €》

『小菜』

〔Android控件知识〕

你可能感兴趣的:(Android控件知识)