当前位置:操作系统 > 安卓/Android >>

android学习笔记8----------数据的存储与访问(2)

承接

android学习笔记6----------数据的存储与访问(1)

http://www.zzzyk.com/kf/201202/119866.html


最后:

Activity的设计。

效果图:

 \

 

import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 
 
import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.Toast; 
 
import com.luku.file.service.FileService; 
 
public class fileActivity extends Activity 

    private EditText editText=null;  
    private TextView content =null;  
    private TextView textView1=null; 
    private Button   read=null; 
    private Button   button =null; 
    private Button   exit=null; 
     
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
         
        textView1=(TextView)findViewById(R.id.textView1); 
        content=(TextView)findViewById(R.id.content); 
        editText=(EditText)findViewById(R.id.filename); 
        button=(Button)findViewById(R.id.baocun); 
        read=(Button)findViewById(R.id.read); 
        exit=(Button)findViewById(R.id.exit); 
         
        button.setOnClickListener(listener); 
        read.setOnClickListener(listener);       
        exit.setOnClickListener(listener); 
    } 
     
    private OnClickListener listener=new OnClickListener() 
    { 
        int resId = R.string.Success; 
        @Override 
        public void onClick(View v) 
        { 
            Button btn=(Button)v; 
            switch (btn.getId()) 
            { 
                case R.id.baocun: 
                    try 
                    { 
                        OutputStream stream=openFileOutput(editText.getText().toString(), Context.MODE_PRIVATE); 
                        FileService.save(stream, content.getText().toString()); 
                    }  
                    catch (IOException e) 
                    { 
                        e.printStackTrace(); 
                        resId = R.string.error; 
                        Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show(); 
                    } 
                    Toast.makeText(fileActivity.this, resId, Toast.LENGTH_LONG).show(); 
                    break; 
                     
                case R.id.read: 
                    try 
                    { 
                        InputStream instream =openFileInput(editText.getText().toString()); 
                        String string=FileService.read(instream); 
                        System.out.println(string); 
                        textView1.setText(string.toString()); 
                    }  
                    catch (Exception e) 
                    { 
                        Toast.makeText(fileActivity.this, "读取错误", Toast.LENGTH_LONG).show(); 
                    }        
                 &nb

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,