当前位置: 首页 > news >正文

厦门网站建设屈兴东网络营销的基本流程

厦门网站建设屈兴东,网络营销的基本流程,免费logo设计图,厦门模板建站系统android studio版本:2023.3.1 patch2 例程:readtextviewIDsaveandread 本例程是个过渡例程,如果单是实现下图的目的有更简单的方法,但这个方法是下一步工作的基础,所以一定要做。 例程功能:将两个textvi…

android studio版本:2023.3.1 patch2

例程:readtextviewIDsaveandread

本例程是个过渡例程,如果单是实现下图的目的有更简单的方法,但这个方法是下一步工作的基础,所以一定要做。

例程功能:将两个textview的text保存到文件,再通过一个按钮读出文件内容,并将两个值分别赋值给另两个textview的text.实现修改多个textview的text的目的。

例程演示:

代码:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView2"android:layout_width="150dp"android:layout_height="35dp"android:background="#00BCD4"android:text="Hello World!"android:textSize="24dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.498"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView1"app:layout_constraintVertical_bias="0.099" /><TextViewandroid:id="@+id/textView1"android:layout_width="110dp"android:layout_height="35dp"android:layout_marginTop="288dp"android:background="#00BCD4"android:text="演示内容"android:textSize="24dp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintHorizontal_bias="0.498"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="104dp"android:layout_marginTop="48dp"android:text="存储"android:textSize="20dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView3" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="48dp"android:layout_marginTop="4dp"android:text="读取"android:textSize="20dp"app:layout_constraintStart_toEndOf="@+id/button1"app:layout_constraintTop_toTopOf="@+id/button1" /><TextViewandroid:id="@+id/textView3"android:layout_width="150dp"android:layout_height="35dp"android:layout_marginStart="68dp"android:layout_marginTop="48dp"android:text="TextView"android:textSize="24dp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/textView2" /><TextViewandroid:id="@+id/textView4"android:layout_width="140dp"android:layout_height="35dp"android:layout_marginStart="32dp"android:text="TextView"android:textSize="24dp"app:layout_constraintStart_toEndOf="@+id/textView3"app:layout_constraintTop_toTopOf="@+id/textView3" /></androidx.constraintlayout.widget.ConstraintLayout>

由于“写文件”还是用到了前一篇(android studio 读写文件操作(方法一)(转)-CSDN博客)的方法,所以保留了相关内容,其实不用也行,懒得改。

FileHelper.java

package com.shudu.readtextviewidsaveandread;import android.content.Context;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class FileHelper {private Context mContext;public FileHelper() {}public FileHelper(Context mContext) {super();this.mContext = mContext;}/** 这里定义的是一个文件保存的方法,写入到文件中,所以是输出流* */public void save(String filename, String filecontent) throws Exception {//这里我们使用私有模式,创建出来的文件只能被本应用访问,还会覆盖原文件哦FileOutputStream output = mContext.openFileOutput(filename, Context.MODE_PRIVATE);output.write(filecontent.getBytes());  //将String字符串以字节流的形式写入到输出流中output.close();         //关闭输出流}/** 这里定义的是文件读取的方法* */public String read(String filename) throws IOException {//打开文件输入流FileInputStream input = mContext.openFileInput(filename);byte[] temp = new byte[1024];StringBuilder sb = new StringBuilder("");int len = 0;//读取文件内容:while ((len = input.read(temp)) > 0) {sb.append(new String(temp, 0, len));}//关闭输入流input.close();return sb.toString();}}

mainactivity.java

package com.shudu.readtextviewidsaveandread;import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.charset.Charset;public class MainActivity extends AppCompatActivity implements View.OnClickListener {private TextView textview1,textview2,textview3,textview4;private Button button1,button2;private Context mContext;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);EdgeToEdge.enable(this);setContentView(R.layout.activity_main);mContext = getApplicationContext();//string的上下文对象,我也不知道是啥,没它不行。ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);return insets;});textview1=(TextView)findViewById(R.id.textView1);textview2 = (TextView) findViewById(R.id.textView2);textview3 = (TextView) findViewById(R.id.textView3);textview4 = (TextView) findViewById(R.id.textView4);button1 = (Button) findViewById(R.id.button1);button2 = (Button) findViewById(R.id.button2);button1.setOnClickListener(this);button2.setOnClickListener(this);}public void onClick(View view) {switch (view.getId()){case R.id.button1://写FileHelper fHelper = new FileHelper(mContext);String filename = "10000.txt";String filedetail = textview1.getText().toString();String filedetail1= textview2.getText().toString();String newstring=filedetail+","+filedetail1;try {fHelper.save(filename, newstring);System.out.println("文件名为:"+filename);Toast.makeText(getApplicationContext(), "数据写入成功", Toast.LENGTH_SHORT).show();} catch (Exception e) {e.printStackTrace();Toast.makeText(getApplicationContext(), "数据写入失败", Toast.LENGTH_SHORT).show();}break;case R.id.button2://读//FileHelper fHelper2 = new FileHelper(getApplicationContext());readFileAndSplit();break;}}@Overridepublic void onPointerCaptureChanged(boolean hasCapture) {}private void readFileAndSplit(){File file =new File(getFilesDir(),"10000.txt");//这个必须单独写,不能直接写到try里面,不知道为啥。try(BufferedReader reader=new BufferedReader(new FileReader(file))){String line=reader.readLine();//读取行String[] parts=line.split(",");//split按照“,”分割,并写进part1数组String str1=parts[0].trim();//读数组第一个值,trim是去年后面空格。String str2=parts[1].trim();//读数组第二个值textview3.setText(str1);textview4.setText(str2);} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}
}

 中间保存文件位置参照前一篇(方法一)。

有关this,switch的R.id错误,参照android studio 按钮点击事件的实现方法(三种方法)_安卓按钮点击事件-CSDN博客

相关内容修改。

http://www.hkea.cn/news/107726/

相关文章:

  • 成都圣都装饰装修公司北京搜索优化排名公司
  • 境外建设网站贴吧互联网域名注册查询
  • 广州建站工作室淘客推广怎么做
  • 中国最大的网站建设公司百度广告联盟点击一次多少钱
  • wordpress单页主题营销seo手机关键词网址
  • dedecms做电影网站韩国最新新闻
  • 哪个网站做废旧好如何在百度上发布自己的广告
  • 网站表单及商品列表详情模板如何搭建自己的网站
  • 网站域名登记证明百度高级搜索怎么用
  • 国外网站在国内做镜像站点网站搭建费用
  • 网站后台如何添加关键词软件开发公司
  • 手机做网站的网站windows优化大师卸载不了
  • 万网速成网站有哪些 功能自己的网站怎么推广
  • 邯郸哪有做网站的河南百度推广公司
  • 我是做环保类产品注册哪些浏览量大的网站推销自己的产品比较好呢西安网站seo优化公司
  • 网页传奇游戏排行昆明网络推广优化
  • 商城模板网站模板网站软文是什么
  • 校园网站推广方案怎么做网站排名推广工具
  • 深圳罗湖企业网站建设报价网络媒体发稿平台
  • 用别人公司域名做网站线下推广的渠道和方法
  • php mysql的网站开发外贸推广平台
  • 济南网站建设认可搜点网络能百度指数有三个功能模块
  • 网上商城网站建设意义在线代理浏览网页
  • 网站图片切换代码百度下载并安装最新版
  • 微信公众平台号申请注册入口杭州seo公司
  • 本周实时热点新闻事件seo文章代写一篇多少钱
  • 旺店通app手机企业版下载网站seo如何优化
  • 宝山区建设用地事务所网站网络公司有哪些
  • 用sql做简单的博客网站大连谷歌seo
  • 新手怎么学做网站就业培训机构有哪些