通过键盘输入

时间:2016-07-21 15:53:13

标签: android text-files

我是android studio的新手,并按照教程成功构建了一个键盘。我现在正在尝试捕获带时间戳的事件。因此,当按下按钮时,我会得到时间戳。我尝试使用SQLite数据库但是遇到了问题。我现在正在尝试创建一个文本文件并将数据保存到文件中。我想知道是否有人能够帮助我。

我对SO进行了一些研究,并且只找到了将数据保存到外部SD卡的文档,当我尝试修改代码时,它没有用。

import android.content.Context;
import android.inputmethodservice.InputMethodService;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.media.AudioManager;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.InputConnection;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class SimpleIME extends InputMethodService
        implements KeyboardView.OnKeyboardActionListener {

    private KeyboardView kv;
    private Keyboard keyboard;

    private boolean caps = false;

    @Override
    public View onCreateInputView() {

        kv = (KeyboardView)getLayoutInflater().inflate(R.layout.keyboard, null);
        keyboard = new Keyboard(this, R.xml.qwerty);
        kv.setKeyboard(keyboard);
        kv.setOnKeyboardActionListener(this); 
        return kv;
    }

    private void playClick(int keyCode){
        AudioManager am = (AudioManager)getSystemService(AUDIO_SERVICE);
        switch(keyCode){
            case 32:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_SPACEBAR);
                break;
            case Keyboard.KEYCODE_DONE:
            case 10:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_RETURN);
                break;
            case Keyboard.KEYCODE_DELETE:
                am.playSoundEffect(AudioManager.FX_KEYPRESS_DELETE);
                break;
            default: am.playSoundEffect(AudioManager.FX_KEYPRESS_STANDARD);
        }
    }

    @Override
    public void onKey(int primaryCode, int[] keyCodes) {
        InputConnection ic = getCurrentInputConnection();
        playClick(primaryCode);
        switch(primaryCode){
            case Keyboard.KEYCODE_DELETE :
                ic.deleteSurroundingText(1, 0);
                break;
            case Keyboard.KEYCODE_SHIFT:
                caps = !caps;
                keyboard.setShifted(caps);
                kv.invalidateAllKeys();
                break;
            case Keyboard.KEYCODE_DONE:
                ic.sendKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_ENTER));
                break;
            default:
                char code = (char)primaryCode;
                if(Character.isLetter(code) && caps){
                    code = Character.toUpperCase(code);
                }
                ic.commitText(String.valueOf(code),1);
        }
    }

2 个答案:

答案 0 :(得分:1)

要简单地将一些文本写入文件,您可以使用Java内置的FileWriter或FileOutputStream。

try {
       //object is the JSON file.
       JSONArray Journey = object.getJSONArray("journeys");
       if (Journey != null) {
        //Retrieving number of possible routes.
        for (int i=0;i<Journey.length();i++){
             Routes.add(Journey.getJSONObject(i));
        }
        //Retrieving number of possible legs for each route.
        if (!Routes.isEmpty()){
        for (int j = 0; j< Routes.size(); j++){
             Legs.add(j, Routes.get(j).getJSONArray("legs"));
        }
        //Trying to retrieve the detailed instruction here and failing.
        for(int k=0;k<Routes.get(k).getJSONArray("legs").length();k++){
            instructionDetail.add(k,Legs.get(k).getJSONObject(k).getJSONObject("instruction"));

        }
     }
 }

} catch (JSONException e) {
                    e.printStackTrace();
              }

之前已经回答过:How do I write to a .txt file in Android?

要获得时间戳,我建议使用Calendar和SimpleDateFormat。

File root = new File(DIRECTORY_PATH);
File mFile = new File(root, "samples.txt");
FileWriter writer = new FileWriter(gpxfile);
writer.append("String to write.");
writer.flush();
writer.close();

答案 1 :(得分:0)

关于写入内部存储空间,请查看official docs about writing files to the internal storage。基本上,您在应用程序专用文件夹中编写了一个文件,只有您的应用程序才能访问该文件(它通常位于&#34; / data / data /&#34;目录中)。正如文档所说,你将使用&#34; getFilesDir()&#34;为了获取您的私人目录路径,您使用FileOutputStream编写文件,并且它取决于您要编写文件的格式。