zoukankan      html  css  js  c++  java
  • android:file

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/name" />
    
        <EditText
            android:id="@+id/fname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="text" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/content" />
    
        <EditText
            android:id="@+id/fcontent"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:minLines="6" />
    
        <Button
            android:id="@+id/store"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/store" />
    
    </LinearLayout>


    package com.example.service;
    
    import java.io.FileOutputStream;
    
    import android.content.Context;
    
    public class FileService {
    	private Context context;
        public FileService(Context context) {
    		this.context = context;
    	}
    	public void save(String filename,String content)throws Exception{
        	FileOutputStream outputStream=context.openFileOutput(filename, Context.MODE_PRIVATE);
        	outputStream.write(content.getBytes());
        	outputStream.close();
        }
    }
    

    package com.example.file;
    
    import com.example.service.FileService;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class FileActivity extends Activity {
    
    	private static final String TAG="FileActivity";
    	private FileService fileService;
    	@Override
    	protected void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_file);
    	fileService=new FileService(this);
    	Button button=(Button)this.findViewById(R.id.store);
    	button.setOnClickListener(new View.OnClickListener() {
    		
    		@Override
    		public void onClick(View v) {
                EditText filenameText=(EditText)findViewById(R.id.fname);
                EditText contentText=(EditText)findViewById(R.id.fcontent);
                String filename=filenameText.getText().toString();
                String content=contentText.getText().toString();
                try {
    				fileService.save(filename, content);
    				Toast.makeText(FileActivity.this, R.string.success,Toast.LENGTH_LONG).show();
    			} catch (Exception e) {
    				Log.e(TAG,e.toString());
    				Toast.makeText(FileActivity.this, R.string.fail,Toast.LENGTH_SHORT).show();
    			}
    		}
    	});
    	}
    }
    


  • 相关阅读:
    table中tr间距的设定table合并单元格 colspan(跨列)和rowspan(跨行)
    使用jquery触发a标签跳转
    真正的让iframe自适应高度 兼容多种浏览器随着窗口大小改变
    html5 data属性的使用
    jQuery取得select选择的文本与值
    jqueryui教程
    密码复杂度
    zabbix配置微信报警
    tomcat配置域名访问
    阿里云ecs禁止ping,禁止telnet
  • 原文地址:https://www.cnblogs.com/javafly/p/6037234.html
Copyright © 2011-2022 走看看