Android 是否将EditText/TextView转换为字符串?
我们的示例代码Android 是否将EditText/TextView转换为字符串?,android,string,textview,android-edittext,Android,String,Textview,Android Edittext,我们的示例代码 EditText txtData = (EditText) findViewById(R.id.txtData); Button btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile); btnReadSDFile.setOnClickListener(new OnClickListener() { public void onClick(View v) { // write on SD card
EditText txtData = (EditText) findViewById(R.id.txtData);
Button btnReadSDFile = (Button) findViewById(R.id.btnReadSDFile);
btnReadSDFile.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// write on SD card file data in the text box
try {
//Read a file from SDCard to TextBox(EditText)
File myFile = new File("/mnt/sdcard/config_data.med");
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
txtData.setText(aBuffer);
myReader.close();
String s = txtData.getText().toString();
Log.e("txtData",s);
}catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
我们的原始文件内容
Welcome
To
All
我们的日志结果截图
为什么我们的内容在这里崩溃?我们如何解决它?这应该是一个编码问题。试试这个:
FileInputStream fIn = new FileInputStream(myFile);
BufferedReader myReader = new BufferedReader(new InputStreamReader(fIn, "UTF-8"));
引用不同的编码字符集
我收到一条错误消息
构造函数文件inputstream(文件,字符串)未定义
我编辑了我的答案。有个书写错误。我很抱歉!字符串被传递给InputStreamReader构造函数,而不是FileInputStrem构造函数。谢谢。这是一个编码问题。但是你已经得到了一个作为字符串的aBuffer值,为什么要将它设置为edittext,然后再次尝试从edittext获得相同的值。尝试将aBuffer打印到日志中,看看会发生什么??好问题。我尝试不使用EditText
和String
,也得到了相同的答案。