Android-IO

3106 단어
1.  SD     
    <!-- SD   ,       -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- SD       -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
2.Main.xml
<!-- IO     -->
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/file_action" />
    
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:id="@+id/filename"/>
    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="    " />
    
    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        android:minLines="3"
        android:id="@+id/file_content"/>
    
    <Button android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="  "
        android:id="@+id/file_btn"
        />
3.string.xml
  <!-- IO     -->
    <string name="file_action">    </string>
    <string name="filename">       </string>
    <string name="success">    </string>
    <string name="error">    </string>
    <string name="sdcardError">SD            </string>

4.event
 private final class FileBtnEvent implements View.OnClickListener{

    public void onClick(View v) {
        EditText fileNameText=(EditText) findViewById(R.id.filename);
        EditText fileConText=(EditText) findViewById(R.id.file_content);
        String filename=fileNameText.getText().toString();
        String fileContent=fileConText.getText().toString();
        FileService service=new FileService(getApplicationContext());
        /*//        
         * try {
            service.save(filename,fileContent);
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),R.string.error,1).show();
        }
        //      
        Toast.makeText(getApplicationContext(),R.string.success,1).show();
        */
        
        //SD    
        try {
            //  SD           
            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
                service.saveToSDCard(filename,fileContent);    
                Toast.makeText(getApplicationContext(),R.string.success,1).show();
            }else{
                //SD           
                Toast.makeText(getApplicationContext(),R.string.sdcardError,1).show();
            }
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(),R.string.error,1).show();
        }
        
    }
5.MainActvity.java
 //IO    
 Button fileBtn=(Button)this.findViewById(R.id.file_btn);
 fileBtn.setOnClickListener(new FileBtnEvent());

좋은 웹페이지 즐겨찾기