配置文件?AndroidManifest.xml 如下
Version:1.0StartHTML:000000243
EndHTML:000019067
StartFragment:000007811
EndFragment:000018976
StartSelection:000007815
EndSelection:000018966
SourceURL:https://github.com/lqr41710085/App1/blob/master/app/src/main/AndroidManifest.xml
xmlns:dist="http://schemas.android.com/apk/distribution"
package="com.example.app1">
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
連接網(wǎng)絡(luò):
publiccpublicclassMainActivityextendsAppCompatActivityimplementsRunnable{lassMainActivityextendsAppCompatActivityimplementsRunnable{
doubledol,eur,jap,hk;
TextViewedit,text;
privatestaticfinalStringTAG="MainActivity";
SharedPreferencessp;
SharedPreferences.Editoreditor;
Handlerhandler;
doubledol,eur,jap,hk;
TextViewedit,text;
privatestaticfinalStringTAG="MainActivity";
SharedPreferencessp;
SharedPreferences.Editoreditor;
Handlerhandler;
@Override
publicvoidrun() {//獲取網(wǎng)絡(luò)數(shù)據(jù)必須另建一個(gè)線程
Documentdoc=null;
Bundlebd=newBundle();
try{
Stringurl="https://www.usd-cny.com/bankofchina.htm";
doc=Jsoup.connect(url).get();
//Log.i(TAG,"hhhhhdoc:"+doc.title());
Elementstables=doc.getElementsByTag("table");
Elementtable=tables.first();
Elementstds=table.getElementsByTag("td");
Elementsps=doc.getElementsByTag("p");
Elementp=ps.get(0);
bd.putString("time",p.text());
//Log.i(TAG,"hhhh"+p.text());//獲取時(shí)間裝入bundle
for(inti=0;i<tds.size();i+=6){
Elementtd1=tds.get(i);
Elementtd2=tds.get(i+5);
Stringname=td1.text();
Stringrate=td2.text();
if(name.equals("美元")||name.equals("日元")||name.equals("歐元")||name.equals("港幣")){
floatv=100f/Float.parseFloat(rate);
bd.putFloat(name,v);
//Log.i(TAG,name+"hhhhh: "+v+"\n");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
}catch(IOExceptione) {
e.printStackTrace();
? ? ? ? }
Messagemsg=handler.obtainMessage(4);
msg.obj=bd;
handler.sendMessage(msg);
//Log.i(TAG,"hhhhsend");
? ? }
privateStringinputStream2String(InputStreaminputStream)throwsIOException{
Stringstr="";
BufferedReaderreader=newBufferedReader(newInputStreamReader(inputStream,"gb2312"));
StringBuffersb=newStringBuffer();
while((str=reader.readLine())!=null){
Log.i(TAG,"in2str:"+str);
sb.append(str).append("\n");
? ? ? ? }
returnsb.toString();
? ? }
@Override
protectedvoidonCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Threadt=newThread(this);
t.start();
sp=this.getSharedPreferences("rate",MODE_PRIVATE);
handler=newHandler(){
publicvoidhandleMessage(Messagemsg){
if(msg.what==4){
Bundlebd2=(Bundle)msg.obj;
//判斷時(shí)間是否與上次寫入時(shí)間相同,不同則更新,相同則為同一天,不更新
Stringlasttime=sp.getString("time","0000-00-00");
Stringtime=bd2.get("time").toString().substring(12,22);
Log.i(TAG,lasttime+"hhhh"+time);
if(!time.equals(lasttime)){
editor=sp.edit();
editor.putFloat("dol",bd2.getFloat("美元"));
editor.putFloat("eur",bd2.getFloat("歐元"));
editor.putFloat("jap",bd2.getFloat("日元"));
editor.putFloat("hk",bd2.getFloat("港幣"));
editor.putString("time",time);
editor.apply();
Log.i(TAG,"hhh已更新"+bd2.toString());
? ? ? ? ? ? ? ? ? ? }
else
Log.i(TAG,"同一天,不更新hhhh");
? ? ? ? ? ? ? ? }
super.handleMessage(msg);
? ? ? ? ? ? }
? ? ? ? };
dol=sp.getFloat("dol",0.0f);
eur=sp.getFloat("eur",0.0f);
jap=sp.getFloat("jap",0.0f);
hk=sp.getFloat("hk",0.0f);
/*dol=0.147;
? ? ? ? eur=0.125;
? ? ? ? jap=15.372;
? ? ? ? hk=1.149;
*/
? ? }
//在xml中btn綁定事件,四個(gè)按鈕可以綁定一個(gè)事件,再依次判斷
publicvoidbtn(Viewbtn){
edit=findViewById(R.id.editText);
text=findViewById(R.id.textView3);
if(edit.getText().toString().equals("")){
Toast.makeText(this,"pls enter a number",Toast.LENGTH_SHORT).show();
? ? ? ? }
elseif(btn.getId()==R.id.button){
doublen=Double.parseDouble(edit.getText().toString());
n=n*dol;
Strings="after transform"+String.format("%.4f",n).toString()+"美元";
text.setText(s);
? ? ? ? }
elseif(btn.getId()==R.id.button2){
doublen2=Double.parseDouble(edit.getText().toString());
n2=n2*eur;
Strings2="after transform"+String.format("%.2f",n2).toString()+"歐元";
text.setText(s2);
? ? ? ? }
elseif(btn.getId()==R.id.button3){
doublen3=Double.parseDouble(edit.getText().toString());
n3=n3*jap;
Strings3="after transform"+String.format("%.2f",n3).toString()+"日元";
text.setText(s3);
? ? ? ? }
elseif(btn.getId()==R.id.button4) {
doublen4=Double.parseDouble(edit.getText().toString());
n4=n4*hk;
Strings4="after transform"+String.format("%.2f",n4).toString()+"港幣";
text.setText(s4);
? ? ? ? }
? ? }
publicvoidconfig(Viewv){
Intentin=newIntent(this,Main2Activity.class);
/*in.putExtra("dol",dol);
? ? ? ? in.putExtra("eur",eur);
? ? ? ? in.putExtra("jap",jap);
? ? ? ? in.putExtra("hk",hk);
*/
//Log.i(TAG,"config: okhhh");
startActivityForResult(in,1);
? ? }
@Override
protectedvoidonActivityResult(intrequestCode,intresultCode,@NullableIntentdata) {
if(requestCode==1&&resultCode==4){
/*Bundle bdl=data.getExtras();
? ? ? ? ? ? dol=bdl.getDouble("dol",1.0f);
? ? ? ? ? ? eur=bdl.getDouble("eur",1.0f);
? ? ? ? ? ? jap=bdl.getDouble("jap",1.0f);
? ? ? ? ? ? hk=bdl.getDouble("hk",1.0f);
*/
dol=sp.getFloat("dol",0.0f);
eur=sp.getFloat("eur",0.0f);
jap=sp.getFloat("jap",0.0f);
hk=sp.getFloat("hk",0.0f);
Log.i(TAG,"dol:"+dol);
super.onActivityResult(requestCode, resultCode, data);
? ? ? ? }
? ? }
@Override
publicbooleanonCreateOptionsMenu(Menumenu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
returntrue;
? ? }
@Override
publicbooleanonOptionsItemSelected(MenuItemitem) {
//Handle action bar item clicks here. The action bar will
//automatically handle clicks on the Home/Up button, so long
//as you specify a parent activity in AndroidManifest.xml.
intid=item.getItemId();
//noinspection SimplifiableIfStatement
if(id==R.id.about) {
Toast.makeText(this,"i am about",Toast.LENGTH_SHORT).show();
? ? ? ? }
if(id==R.id.settings) {
Toast.makeText(this,"i am settings",Toast.LENGTH_SHORT).show();
? ? ? ? }
returntrue;
? ? }
}