首页 文章 精选 留言 我的

精选列表

搜索[笔记],共9158篇文章
优秀的个人博客,低调大师

Android开发学习笔记:数据存取之Preference浅析

一.Preference的简介 Preference(配置)提供了一种轻量级的数据存取方法,主要应用于数据比较少的配置信息。它以“key-value”(是一个Map)对的方式将数据保存在一个XML配置文件中,例如,手机的开机问候语,可以将其以Preference方式来进行配置。也可以保存一些用户个性化设置的字体、颜色、位置等参数信息。 二.Preference存取数据的方法 使用到的接口: SharedPreferences接口和SharedPreferences.Editor接口,它们都是来自于andorid.content包。 ①SharedPreferences接口提供保存数据的方法 我们可以调用Context.getSharedPreferences(String name,int mode)方法得到SharedPreferences接口。该方法的第一个参数是文件名称,第二个参数是操作模式。操作模式有三种: MODE_PRIVATE(私有) MODE_WORLD_READABLE(可读) MODE_WORLD_WRITEABLE(可写) SharedPreferences接口的常用方法 方法名称 方法描述 edit() 返回SharedPreferences的内部接口SharedPreferences.Editor contains(String key) 判断是否包含该键值 getAll() 返回所有配置信息Map getBoolean(String key,Boolean defValue) 获得一个boolean值 getFloat(String key,float defValue) 获得一个float值 getInt(String key,int defValue) 获得一个int值 getLong(String key,long defValue) 获得一个long值 getString(String key,String defValue) 获得一个String值 ②SharedPreferences.Editor接口提供获得数据的方法 SharedPreferences.Editor接口中的getString(String key,String defValue)、getInt(String key,int defValue)等。调用SharedPreferences的edit()方法返回SharedPreferences.Editor内部接口,该接口中提供了保存数据的方法,如putString(String key,String value)、pubInt(String key,int value)等,调用该接口的commit方法可以将数据进行保存。 SharedPreferences.Editor接口常用的方法 方法名称 方法描述 clear() 清楚所有值 commit() 保存 getAll() 返回所有配置信息Map putBoolean(String key,Boolean value) 保存一个boolean值 putFloat(String key,float value) 保存一个float值 putInt(String key,int value) 保存一个int值 putLong(String key,long value) 保存一个long值 putString(String key,String value) 保存一个String值 remove(String key) 删除该键对应的键 下面是保存临时记事本内容的例子 当我们在手机上用记事本编写备忘录,日志的时候,突然要返回接电话,看短信的时候。我们就要停止编写,退出去听接电话,看短信。然而,当我们打完电话或者看完短信回来的时候,发现刚刚编写的内容还在记事本上,这就是Preference的作用了。 创建一个Activity,在其中放置一个EditText保存短信内容,放置一个Button用于发送信息。在onCreate()中通过getSharedPreferences()方法获得SharedPreferences接口,调用接口的getString()方法,获得保存内容,将内容设置到EditText中。 在onStop()方法中保存内容,使用getSharedPreferences().edit()方法获得SharedPreferences.Editor接口,调用SharedPreferences.Editor的putString()方法保存短信内容,调用commit()方法提交内容。 MainActivity.java packagecom.android.preference; importandroid.app.Activity; importandroid.content.SharedPreferences; importandroid.os.Bundle; importandroid.widget.Button; importandroid.widget.EditText; publicclassMainActivityextendsActivity{ privateEditTextmyEditText; privateButtonbtn; privatestaticfinalStringTEMP_INFO="temp_info"; @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main); myEditText=(EditText)findViewById(R.id.EditText1); btn=(Button)findViewById(R.id.Button1); //获得SharedPreferences实例 SharedPreferencessp=getSharedPreferences(TEMP_INFO,MODE_WORLD_READABLE); //从SharedPreferences获得备忘录的内容 Stringcontent=sp.getString("info_content",""); //在EditText中显示备忘录内容 myEditText.setText(content); } @Override protectedvoidonStop(){ super.onStop(); //获得编辑器 SharedPreferences.Editoreditor=getSharedPreferences(TEMP_INFO,MODE_WORLD_WRITEABLE).edit(); //将EditText中的文本内容添加到编辑器 editor.putString("info_content",myEditText.getText().toString()); //提交编辑器内容 editor.commit(); } } main.xml <?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="备忘录" /> <EditText android:text="" android:id="@+id/EditText1" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/Button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="保存" /> </LinearLayout> 效果图: 当我们退出屏幕再返回的时候,发现原来备忘录里编辑了一半的内容还在那里,其实它是把要保存的内容做诶XML文件保存在/data/data/package/shsared_prefs/下面了。 本文转自 lingdududu 51CTO博客,原文链接:http://blog.51cto.com/liangruijun/658521

优秀的个人博客,低调大师

Text-to-speech 入门与进阶学习笔记(android)

1,入门:让TEXT朗读起来 //1,先写一个用来检查tts是否安装的方法privatefinalstaticintCHECK_CODE=1; publicvoidcheckTts(){ IntentcheckIntent=newIntent();checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA); startActivityForResult(checkIntent,CHECK_CODE);} //创建一个TTS privateTextToSpeechmTts; //这个自己先去了解下onActivityResult是怎么回事,这里不作详解 @OverrideprotectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){ //TODOAuto-generatedmethodstubsuper.onActivityResult(requestCode,resultCode,data); if(requestCode==CHECK_CODE){ if(resultCode==TextToSpeech.Engine.CHECK_VOICE_DATA_PASS){ //成功创建一个TTS mTts=newTextToSpeech(this,this); }else{ //否则安装一个 IntentinstallIntent=newIntent();installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);startActivity (installIntent);}}} //2,让TTS朗读起来 //onCreate方法中 //检查TTS是否正常checkTts(); //创建用组件privatespeakValue=(EditText) findViewById(R.id.speakValue); privatespeakButton=(Button) findViewById(R.id.sayHello); speakButton.setOnClickListener(newOnClickListener(){ @Override publicvoidonClick(Viewv){ //TODOAuto-generatedmethodstub Stringtext=speakValue.getText().toString(); Log.d("result","text--&gt;"+text);sayTts(text);}}); //TTs朗读用privatevoidsayTts(Stringtext){ //最简单的例子mTts.speak(text,TextToSpeech.QUEUE_FLUSH,null); } //补充,使用TTS朗读,一定要实现OnInitListener 接口 //TTs对象创建后初始化 @OverridepublicvoidonInit(intstatus){ if(status==TextToSpeech.SUCCESS){ //设置语言区域intresult=mTts.setLanguage(Locale.US); //如果是不支持语言 if(result==TextToSpeech.LANG_MISSING_DATA||result==TextToSpeech.LANG_NOT_SUPPORTED){ Log.e("error","不支持");}}} 就这么点代码就可以正常运行了… 2,进阶使用:改变我们朗读播放的类型,回调函数的使用与自定义文字发音 1,更改播放的流类型(暂且这样说,希望,有人解释一下) //更改播放使用的流类型 //在sayTts作以下修改 //TTs朗读用privatevoidsayTts(Stringtext){HashMap<String,String>myAlarm=newHashMap(); //把播放类型,通过闹钟流实现myHashAlarm.put(TextToSpeech.Engine.KEY_PARAM_STREAM,String.valueOf(AudioManager.STREAM_ALARM)); //你播放的tts在音频播放的闹钟类型流中mTts.speak(text,TextToSpeech.QUEUE_FLUSH,myAlarm); //英语原文是这样,我了解的就是以上我注释的内容 //我感觉不是很了解,希望有人帮帮 //OnAndroid,eachaudiostreamthatisplayedisassociatedwithone //streamtype,asdefinedinandroid.media.AudioManager.Foratalking //alarmclock,wewouldlikeourtexttobeplayedonthe //AudioManager.STREAM_ALARMstreamtypesothatitrespectsthealarm //settingstheuserhaschosenonthedevice. } 2,回调函数的使用 //在sayTts()中实现.. //需要实现OnUtteranceCompletedListener这个接口 //TTs朗读用 privateToasttoast;prvateContextcontext=getApplicationContext(); privatevoidsayTts(Stringtext){HashMap<String,String>myAlarm=newHashMap();mTts.setOnUtteranceCompletedListener(this); myAlarm.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"metoo"); toast=Toast.makeText(context,"metoo",Toast.LENGTH_LONG); //最简单的例子mTts.speak(text,TextToSpeech.QUEUE_FLUSH,myAlarm);} //回调函数@OverridepublicvoidonUtteranceCompleted(StringutteranceId){speakPlayBack(utteranceId);} privatevoidspeakPlayBack(Stringstr){Log.d("result","playBack-->"+str);toast.show();} 3,录制TTS朗读保存到SD卡中 //把TTS朗读结果保存 privatevoidsayTofile(Stringtext){ HashMap<String,String>ttsRender=newHashMap<String,String>(); StringdestFileName="/sdcard/tts/"+text+".wav";ttsRender.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,text);mTts.synthesizeToFile(text,ttsRender,destFileName); } //在 你就可以看到你刚才朗读的Text被保存了 4,自定义Text发音 //自定义文字播放 privatevoidsayTrue(Stringtext){ StringdestFileName="/sdcard/tts/"+text+".wav";mTts.addSpeech(text,destFileName); mTts.speak(text,TextToSpeech.QUEUE_FLUSH,null);} //在onclick方法中运行sayTrue(); 亲测,MP3也可以播放… 5,销毁 @OverrideprotectedvoidonDestroy(){super.onDestroy(); if(mTts!=null){ mTts.stop(); mTts.shutdown();}}

优秀的个人博客,低调大师

Android开发学习笔记:5大布局方式详解

Android中常用的5大布局方式有以下几种: 线性布局(LinearLayout):按照垂直或者水平方向布局的组件。 帧布局(FrameLayout):组件从屏幕左上方布局组件。 表格布局(TableLayout):按照行列方式布局组件。 相对布局(RelativeLayout):相对其它组件的布局方式。 绝对布局(AbsoluteLayout):按照绝对坐标来布局组件。 1. 线性布局 线性布局是Android开发中最常见的一种布局方式,它是按照垂直或者水平方向来布局,通过“android:orientation”属性可以设置线性布局的方向。属性值有垂直(vertical)和水平(horizontal)两种。 常用的属性: android:orientation:可以设置布局的方向 android:gravity:用来控制组件的对齐方式 layout_weight:控制各个组件在布局中的相对大小 第一个实例 ①效果图: ②核心代码如下: main.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="right" > <!--android:gravity="right"表示Button组件向右对齐--> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="确定" /> <Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="取消" /> </LinearLayout> </LinearLayout> 第二个实例 ①效果图: ②核心代码: mian.xml <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="red" android:gravity="center_horizontal" android:background="#aa0000" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" /> <!--android:gravity="center_horizontal"水平居中--> <!--layout_weight属性以控制各个控件在布局中的相对大小。layout_weight属性是一个非负整数值。 线性布局会根据该控件layout_weight值与其所处布局中所有控件layout_weight值之和的比值为该控件分配占用的区域。 例如,在水平布局的LinearLayout中有两个Button,这两个Button的layout_weight属性值都为1, 那么这两个按钮都会被拉伸到整个屏幕宽度的一半。如果layout_weight指为0,控件会按原大小显示,不会被拉伸; 对于其余layout_weight属性值大于0的控件,系统将会减去layout_weight属性值为0的控件的宽度或者高度, 再用剩余的宽度或高度按相应的比例来分配每一个控件显示的宽度或高度--> <TextView android:text="Teal" android:gravity="center_horizontal" android:background="#008080" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1"/> <TextView android:text="blue" android:gravity="center_horizontal" android:background="#0000aa" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" /> <TextView android:text="orange" android:gravity="center_horizontal" android:background="#FFA500" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1" /> </LinearLayout> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"> <TextView android:text="rowone" android:textSize="15pt" android:background="#aa0000" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <!----> <TextView android:text="rowtwo" android:textSize="15pt" android:background="#DDA0DD" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:text="rowthree" android:textSize="15pt" android:background="#008080" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <TextView android:text="rowfour" android:textSize="15pt" android:background="#FFA500" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout> </LinearLayout> 2. 帧布局 帧布局是从屏幕的左上角( 0,0)坐标开始布局,多个组件层叠排列,第一个添加的组件放到最底层,最后添加到框架中的视图显示在最上面。上一层的会覆盖下一层的控件。 简单的例子 ①效果图: ②核心代码: main.xml <?xmlversion="1.0"encoding="utf-8"?> <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="300dp" android:layout_height="300dp" android:background="#00BFFF" /> <TextView android:layout_width="260dp" android:layout_height="260dp" android:background="#FFC0CB" /> <TextView android:layout_width="220dp" android:layout_height="220dp" android:background="#0000FF" /> </FrameLayout> 3. 表格布局 表格布局是一个 ViewGroup以表格显示它的子视图(view)元素,即行和列标识一个视图的位置。 表格布局常用的属性如下: android:collapseColumns:隐藏指定的列 android:shrinkColumns:收缩指定的列以适合屏幕,不会挤出屏幕 android:stretchColumns:尽量把指定的列填充空白部分 android:layout_column:控件放在指定的列 android:layout_span:该控件所跨越的列数 简单的列子: ①效果图: ②核心代码: main.xml <?xmlversion="1.0"encoding="utf-8"?> <TableLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TableRow> <Button android:text="Button1" /> <Button android:text="Button2" /> <Button android:text="Button3" /> </TableRow> <TableRow> <Button android:text="Button4" /> <Button android:layout_span="2" android:text="Button5" /> </TableRow> </TableLayout> 4. 相对布局 相对布局是按照组件之间的相对位置来布局,比如在某个组件的左边,右边,上面和下面等。 相对布局常用属性请参考我博客的: http://liangruijun.blog.51cto.com/3061169/631816 简单的例子 ①效果图: ②核心代码: main.xml <?xmlversion="1.0"encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:padding="10px" > <TextView android:id="@+id/tev1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="30dp" android:text="PleaseTypeHere:" /> <EditText android:id="@+id/tx1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/tev1" /> <Button android:id="@+id/btn1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/tx1" android:layout_alignParentRight="true" android:text="确定" /> <Button android:id="@+id/btn2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_below="@id/tx1" android:layout_toLeftOf="@id/btn1" android:layout_marginRight="30dp" android:text="取消" /> </RelativeLayout> 5. 绝对布局 绝对布局通过指定子组件的确切 X,Y坐标来确定组件的位置,在Android2.0 API文档中标明该类已经过期,可以使用FrameLayout或者RelativeLayout来代替。所以这里不再详细介绍。 本文转自 lingdududu 51CTO博客,原文链接:http://blog.51cto.com/liangruijun/632532

优秀的个人博客,低调大师

学习笔记-ELK5安装X-Pack

elk5和之前的版本不太一样,他将之前的三方插件集成到了xpack中,想要使用高级功能,必须安装xpack这个插件,但是这个xpack确实让人很头疼,如果配置不正确,es是收不到logstash传来的数据的,更别提Kibana能展示他了,根本就搜不到索引名称。 一、elasticsearch5按这X-Pack的步骤: 1、elasticsearch安装X-Pack: 在每个集群节点的运行ES_HOME里运行bin/elasticsearch-plugin install x-pack,我这里使用的是yum安装,所以ES_HOME是/usr/share/elasticsearch/。 1 bin /elasticsearch-plugin install x-pack 允许X-Pack创建上面的一系列索引。 2、重启elastcsearch服务 1 systemctlrestartelasticsearch 二、Kibana安装X-Pack 1、安装xpack 进入Kibana的安装目录,这里使用的是yum安装,在/usr/share/kibana下面,运行一下命令,在线安装: 1 2 3 cd /usr/share/kibana bin /kibana-plugin install x-pack systemctlrestartkibana 2、配置Kibana配置文件 将配置文件中下面两项取消注释: 这一个细节必须注意,重中之重,不然Kibana会连接不上es,密码是默认的Kibana登录密码,后续可以修改。 1 2 elasticsearch.username: "elastic" elasticsearch.password: "changeme" 3、登录Kibana 在浏览器上访问http://localhost:5601会出现一下页面,但是会出现认证登录。 三、登录认证 官网是这么说的,安装X-Pack后需要认证,而默认帐号:elastic,密码:changeme 四、添加logstash配置文件 1、添加logstash测试文件 在/etc/logstash/conf.d下面写一个测试文件 1 2 3 4 5 6 7 8 9 10 11 input{ stdin{} } output{ elasticsearch{ hosts=> "10.1.12.169:9200" index=> "test-messages-%{+YYYY.MM.dd}" user=> 'elastic' #需要在添加帐号密码,不然无法将数据传输到elasticsearch上 password=> 'changeme' } } 2、测试文件是否可用 1 2 [root@elkconf.d] #logstash-f/etc/logstash/conf.d/test.conf-t ConfigurationOK 3、测试输入是否成功 测试成功可以打开文件,输入进行测试了 1 2 3 4 [root@elkconf.d] #logstash-f/etc/logstash/conf.d/test.conf [root@elkconf.d] #logstash-f/etc/logstash/conf.d/test.conf Thestdinpluginisnowwaiting for input: helloworld 测试中多顶着es、logstash的日志,出了问题知道错在哪! 技术水平有限,先这么着吧。 本文转自 wangpengtai 51CTO博客,原文链接:http://blog.51cto.com/wangpengtai/1910397,如需转载请自行联系原作者

优秀的个人博客,低调大师

Android笔记: 解决ScrollView嵌套ListView的冲突方法

范例: 1.MainActivity代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 public class MainActivity extends Activity { // 只要在设置ListView的Adapter后调用此静态方法Utility.setListViewHeightBasedOnChildren(listview); // 即可让ListView正确的显示在其父ListView的ListItem中。 // 但是要注意的是,子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout) // 没有重写onMeasure(),所以会在onMeasure()时抛出异常。 private ArrayList<String> dataList; private ListView listview; private ImageView img; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); dataList = new ArrayList<String>(); for ( int i = 0 ; i < 5 ; i++) { String str = "第" + i + "行" ; dataList.add(str); } img = (ImageView) findViewById(R.id.imageView1); listview = (ListView) findViewById(R.id.listView1); listview.setAdapter( new BaseAdapter() { @Override public View getView( int position, View convertView, ViewGroup parent) { LayoutInflater inflater = getLayoutInflater(); View layout = inflater.inflate(R.layout.item, null ); TextView tv = (TextView) layout.findViewById(R.id.tv_item); String str = dataList.get(position); tv.setText(str); return layout; } @Override public long getItemId( int position) { return 0 ; } @Override public Object getItem( int position) { return null ; } @Override public int getCount() { return dataList.size(); } }); Utility.setListViewHeightBasedOnChildren(listview); } } 2.Utility代码 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null ) { // pre-condition return ; } int totalHeight = 0 ; for ( int i = 0 ; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null , listView); listItem.measure( 0 , 0 ); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1 )); listView.setLayoutParams(params); } } 本文转自 glblong 51CTO博客,原文链接:http://blog.51cto.com/glblong/1290605,如需转载请自行联系原作者

优秀的个人博客,低调大师

[Android学习笔记六] Toast 更长时间显示

Android Toast是一种比较特殊的显示信息的机制,其没有焦点,并且显示一段时间之后就自动消失。Android 中提供的显示时长通过Toast.LENGHT_LONG和Toast.LENGTH_SHORT来指定,底层具体代表的时间长度分别是3.5s和2.0s。 本文介绍如何使得Toast显示的时间更长或者指定显示时间长度。 1. 基本用法代码示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /** *Toast默认的长时间显示3.5s */ @OnClick (R.id.one_button) public void onClickOneButton(){ Toast.makeText( this , "HelloWorld" ,Toast.LENGTH_LONG).show(); } /** *Toast默认的短时间显示2.0s */ @OnClick (R.id.two_button) public void onClickTwoButton(){ Toast.makeText( this , "HelloWorld" ,Toast.LENGTH_SHORT).show(); } 2. 通过android.os.CountDownTimer类实现Toast的显示和消失 通过点击下图中的底端左Button使得Toast显示,右Button使得Toast消失。 代码示例: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 /** *使用CountDownTimer实现Toast自定义时间显示 */ @Bind (R.id.three_textView) public TextViewthreeTextView; private ToastmCountDownTimeToast; public CountDownTimercdt= new CountDownTimer( 60 * 1000 , 1000 ){ @Override public void onTick( long millisUntilFinished){ threeTextView.setText( "Time剩余:" +(millisUntilFinished/ 1000 )+ "s" ); if ( null !=mCountDownTimeToast){ mCountDownTimeToast.show(); } } @Override public void onFinish(){ threeTextView.setText( "显示完成" ); if ( null !=mCountDownTimeToast){ mCountDownTimeToast.cancel(); } } }; @OnClick ({R.id.three_button_start,R.id.three_button_cancel}) public void onClickThreeButton(Viewv){ if ( null ==mCountDownTimeToast){ mCountDownTimeToast=makeToast( this ); mCountDownTimeToast.setGravity(Gravity.TOP|Gravity.LEFT, 30 , 30 ); } if (v.getId()==R.id.three_button_start){ cdt.start(); Log.d(TAG, "CountDownTimerstartshow" ); } if (v.getId()==R.id.three_button_cancel){ cdt.cancel(); threeTextView.setText( "取消" ); Log.d(TAG, "CountDownTimercancel" ); } } Toast对象的View布局文件(view_toast.xml) 1 2 3 4 5 6 7 8 9 <? xml version = "1.0" encoding = "utf-8" ?> < TextView xmlns:android = "http://schemas.android.com/apk/res/android" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:background = "#ef3939" android:orientation = "vertical" style = "@android:style/TextAppearance.DeviceDefault.Medium" android:gravity = "center_vertical|center_horizontal" android:padding = "10dp" android:text = "&quot;HelloWorld&quot;" android:textColor = "#f7f9fb" > </ TextView > 创建Toast对象(下文中创建Toast对象使用该方法) 1 2 3 4 5 6 7 public static ToastmakeToast(Contextcontext){ Toasttoast= new Toast(context); toast.setDuration(Toast.LENGTH_SHORT); TextViewview=(TextView)LayoutInflater.from(context).inflate(R.layout.view_toast, null ); toast.setView(view); return toast; } 上述代码通过CountDownTimer实现指定Toast显示时长和控制Toast显示与消失,重构之后代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public void showToast1( final Toasttoast, long duration){ final long SHORT= 2000 ; final long LONG= 3500 ; final long ONE_SECOND= 1000 ; final long d=duration<=SHORT?SHORT:duration>LONG?duration:LONG; new CountDownTimer(Math.max(d,duration),ONE_SECOND){ @Override public void onTick( long millisUntilFinished){ toast.show(); } @Override public void onFinish(){ toast.cancel(); } }.start(); } 3.通过Handler消息机制实现 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public void showToast2( final Toasttoast, long duration){ final long SHORT= 2000 ; final long LONG= 3500 ; final long ONE_SECOND= 1000 ; final long d=duration<=SHORT?SHORT:duration>LONG?duration:LONG; final int what= 1 ; final Handlerhandler= new Handler(){ long remainder=d; @Override public void handleMessage(Messagemsg){ if (msg.what==what){ toast.show(); if (remainder>=ONE_SECOND){ remainder-=ONE_SECOND; sendEmptyMessageDelayed(what,ONE_SECOND); } else { removeMessages(what); } } } }; handler.sendEmptyMessage(what); } 其实这种实现和CountDownTimer实现的机制一样的。 4. Toast的指定时长显示其实就是不断的调用Toast的show()方法,其中show()方法中实现了将Toast对象添加到INotificationManager的Toast队列中去。具体细节可参见Toast(android.widget.Toast)源码。 5. 为什么要给Toast指定显示时长呢? 这或许不是个好主意。Toast的无焦点短时间自动消失机制就是为了给用户显示信息而不会产生较大干扰。如果长时间显示并且具有更多交互和事件处理,这就背离了Toast的本意,这样的场景可以考虑使用Notification,Dialog来实现。 使用Toast场景中更多的是关注其View和显示的位置,这可以实现更多有意思的东西,比如冒泡提示,使用向导等。在实践中则需要根据具体情况慎重选择和合理应用。 本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1717222,如需转载请自行联系原作者

优秀的个人博客,低调大师

docker学习笔记(三)——创建自定义镜像

首先拉取一个镜像,在这里以NGINX为例 1 2 3 4 [root@DockServeropt] #dockerpullnginx [root@DockServeropt] #dockerimages REPOSITORYTAGIMAGEIDCREATEDSIZE nginxlatestb8efb18f159b3weeksago107MB 启动NGINX镜像,映射出端口 1 2 3 4 5 [root@DockServeropt] #dockerrun--namewebserver-d-p80:80nginx 8f62585b370ca34eb8c438adbab0f972e1990cee25000a742c6a2d8e7ee7ba38 [root@DockServeropt] #dockerps CONTAINERIDIMAGECOMMANDCREATEDSTATUSPORTSNAMES 8f62585b370cnginx "nginx-g'daemon..." 6secondsagoUp6seconds0.0.0.0:80->80 /tcp webserver 访问端口,直接用命令行访问, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 [root@DockServeropt] #curlhttp://127.0.0.1 <!DOCTYPEhtml> <html> < head > <title>Welcometonginx!< /title > <style> body{ width:35em; margin:0auto; font-family:Tahoma,Verdana,Arial,sans-serif; } < /style > < /head > <body> <h1>Welcometonginx!< /h1 > <p>Ifyouseethispage,thenginxwebserverissuccessfullyinstalledand working.Furtherconfigurationisrequired.< /p > <p>Foronlinedocumentationandsupportpleasereferto <ahref= "http://nginx.org/" >nginx.org< /a >.<br/> Commercialsupportisavailableat <ahref= "http://nginx.com/" >nginx.com< /a >.< /p > <p><em>Thankyou for usingnginx.< /em >< /p > < /body > < /html > 可以看到能够访问在nginx docker里的东东了,下面我们修改下首页内容 进入nginx docker 里面进行修改 1 2 3 4 5 进入docker [root@DockServeropt] #dockerexec-itwebserverbash root@8f62585b370c:/ #echo'<h1>Hello,Docker!</h1>'>/usr/share/nginx/html/index.html root@8f62585b370c:/ #exit exit 再次访问看看 1 2 [root@DockServeropt] #curlhttp://127.0.0.1 <h1>Hello,Docker!< /h1 > 可以看出,已经修改成功,我们保存成镜像 1 2 3 4 5 6 7 8 9 10 11 12 [root@DockServeropt] #dockercommit--author"DingJin<dingjin@gmail.com>"--message"修改nginx默认网页"webserverwebserver:v2 sha256:ca35d11b57bac6e3e4ebab15aaff528c8530f7f5e59e00f58fa61e86edf1aa91 [root@DockServeropt] #dockerimages REPOSITORYTAGIMAGEIDCREATEDSIZE webserverv2ca35d11b57ba6secondsago107MB nginxlatestb8efb18f159b3weeksago107MB 已经可以看到webserver:v2镜像了,运行下 [root@DockServeropt] #dockerrun--nameweb2-d-p81:80webserver:v2 1647edeba49aa664c0ccf642248f6b30b36c6b08990ede580a5803f1a59ae545 [root@DockServeropt] #curlhttp://127.0.0.1:81 <h1>Hello,Docker!< /h1 > 可以了解以下命令, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 查看webserver做了哪些更改,即和源镜像对比不同 [root@DockServeropt] #dockerdiffwebserver C /root A /root/ .bash_history C /run A /run/nginx .pid C /var C /var/cache C /var/cache/nginx A /var/cache/nginx/uwsgi_temp A /var/cache/nginx/client_temp A /var/cache/nginx/fastcgi_temp A /var/cache/nginx/proxy_temp A /var/cache/nginx/scgi_temp C /usr C /usr/share C /usr/share/nginx C /usr/share/nginx/html 查看webserver:v2历史文件变动记录 [root@DockServeropt] #dockerhistorywebserver:v2 IMAGECREATEDCREATEDBYSIZECOMMENT ca35d11b57ba4minutesagonginx-gdaemonoff;98B修改nginx默认网页 b8efb18f159b3weeksago /bin/sh -c #(nop)CMD["nginx""-g""daem...0B <missing>3weeksago /bin/sh -c #(nop)STOPSIGNAL[SIGTERM]0B <missing>3weeksago /bin/sh -c #(nop)EXPOSE80/tcp0B <missing>3weeksago /bin/sh -c ln -sf /dev/stdout /var/log/ngi ...0B <missing>3weeksago /bin/sh -capt-getupdate&&apt-getinst...52.2MB <missing>3weeksago /bin/sh -c #(nop)ENVNJS_VERSION=1.13.3....0B <missing>3weeksago /bin/sh -c #(nop)ENVNGINX_VERSION=1.13....0B <missing>3weeksago /bin/sh -c #(nop)MAINTAINERNGINXDocker...0B <missing>3weeksago /bin/sh -c #(nop)CMD["bash"]0B <missing>3weeksago /bin/sh -c #(nop)ADDfile:fa8dd9a679f473a...55.2MB 结合上篇,我们上传到本地私有仓库,然后就直接可以在本地调用了~~ 本文转自 jackjiaxiong 51CTO博客,原文链接:http://blog.51cto.com/xiangcun168/1957431

优秀的个人博客,低调大师

docker学习笔记(一)——centos7 安装docker

Docker 最低支持 CentOS 7, 推荐Centos7.3版本 配置yum源 1 2 3 4 5 6 7 #tee/etc/yum.repos.d/docker.repo<<-'EOF'[dockerrepo] name=DockerRepository baseurl=https: //yum .dockerproject.org /repo/main/centos/7/ enabled=1 gpgcheck=1 gpgkey=https: //yum .dockerproject.org /gpg EOF 安装 Docker 更新yum软件源缓存,并安装docker-engine。 1 2 #yumupdate #yuminstalldocker-engine 启动 Docker 引擎 1 2 #systemctlenabledocker #systemctlstartdocker 以上就安装完了docker 本文转自 jackjiaxiong 51CTO博客,原文链接:http://blog.51cto.com/xiangcun168/1957360

优秀的个人博客,低调大师

Android学习笔记--Android开发时常用控件(三)

ProgressBar(进度条) ListView(很重要的显示控件) 例:<ProgressBarandroid:id="@+id/pBar" Style="?Android:attr/progressBarStyleHorizontal"--水平方向的进度条 Android:layout_width="200dp" Android:layout_height="wrap_content" Android:visibility="gone" Android:max=200 /> 代码:1.声明变量 2.根据ID取得控件对象findViewById 3.设置监听类 ClassButtonLinstenerimplementsOnClickListener{ PublicvoidonClick(Viewv){ If(i==0){ pBar.setVisibility(View.VISIBLE); } Elseif(i<pBar.getMax()){ pBar.setProgress(i);--设置主进度条的当前值 pBar.setSecondaryProgress(i+10);--设置第二进度条的当前值 } Else{ pBar.setVisbility(View.GONE); } I=i+10; } } 4.绑定事件 myButton.setOnClickListener(newButtonLinstener()); 例:main布局文件 <ListViewandroid:id="@id/android:list" Android:layout_width="fill_parent" Android:layout_height="wrap_content" Android:drawSelecrorOnTop="false" Android:scrollbars="vertical"/> User布局文件略(两个TextView) 代码:publicclassActivityextendsListActivity{ super.onCreate(savedInstanceState); setContentView(R.layout.main); //用一个集合对象装数据 ArrayList<HashMap<Stirng,String>>list=newArrayList<HashMap<Stirng,String>>(); HashMap<String,String>Map1=newHashMap<String,String>(); HashMap<String,String>Map2=newHashMap<String,String>(); HashMap<String,String>Map3=newHashMap<String,String>(); Map1.put("user_name","zhangsan"); Map1put("user_ip","192.168.0.1"); Map2.put("user_name","lisi"); Map2.put("user_ip","192.168.0.2"); Map3.put("user_name","wangwu"); Map3.put("user_ip","192.168.0.3"); List.add(Map1); List.add(Map2); List.add(Map3); //listActivity对象,map,第二个布局文件,对应hashMap的值对(可想象成列),控件显示的位置 SimpleAdapterlistAdapter=newSimpleAdapter(this,list,R.layout.user,newString[]{"user_name","user_ip"},newint[]{R.id.user_ip,R.id.user_name}); setListAdapter(listAdapter); } //重写 ProtectedvoidonListItemClick(ListViewlv,Viewv,intposition,longid){ super.onListItemClick(1,v,position,id); System.out.println("id--------"+id); System.out.println("position-------"+position) } 本文转自My_King1 51CTO博客,原文链接:http://blog.51cto.com/apprentice/1360573,如需转载请自行联系原作者

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册
修改密码
忘记密码