我是Android应用程序编程的新手。
在下面的代码中,我想通过一些EditTexts从用户那里获取输入,然后将它们保存到一个文件中。我使用android studio并在输入后,检查了文件,它总是空的。我尝试显示内容仍然没有输出。代码有什么问题?
请帮帮我。
public class user extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
nextClick();
}
public void nextClick()
{
final Context context = this;
Button next =(Button) findViewById(R.id.userNext);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
EditText e1 = (EditText) findViewById(R.id.name);
EditText e2 = (EditText) findViewById(R.id.designation);
EditText e3 = (EditText) findViewById(R.id.orgname);
EditText e4 = (EditText) findViewById(R.id.address);
EditText e5 = (EditText) findViewById(R.id.city);
EditText e6 = (EditText) findViewById(R.id.state);
EditText e7 = (EditText) findViewById(R.id.zip);
FileOutputStream fos = openFileOutput("data.txt", Context.MODE_WORLD_WRITEABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(e1.toString());
fos.flush();
fos.close();
}
catch(Exception e) {
}
try
{
FileInputStream fis = new FileInputStream("data.txt");
while(fis.read()!=-1)
{
System.out.println((char)fis.read());
}
}
catch(Exception e)
{
}
Intent intent3 = new Intent(context,com.example.cheruvu.rs.user2.class);
startActivity(intent3);
}
});
}
}
答案 0 :(得分:0)
在您写入文件时,您需要从 EditText 字段 getText(),如下所示
POST /test_index/_search
{
"query": {
"filtered": {
"query": {
"term": {
"name": {
"value": "b"
}
}
},
"filter": {
"limit": {
"value": 2
}
}
}
},
"aggs": {
"val_sum": {
"sum": {
"field": "val"
}
}
}
}
...
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.73895097,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 0.73895097,
"_source": {
"name": "a b",
"val": 2
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "4",
"_score": 0.73895097,
"_source": {
"name": "b a",
"val": 4
}
}
]
},
"aggregations": {
"val_sum": {
"value": 6,
"value_as_string": "6.0"
}
}
}
答案 1 :(得分:0)
这个怎么样?这将在手机上创建文件(而不是SD卡)
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "MyFileName");
FileOutpuStream fos = new FileOutputStream(myFile);
答案 2 :(得分:0)
尝试用此替换您的代码,看看它是否有效
public class user extends Activity {
EditText e1;
EditText e2;
EditText e3;
EditText e4;
EditText e5;
EditText e6;
EditText e7;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
e1 = (EditText) findViewById(R.id.name);
e2 = (EditText) findViewById(R.id.designation);
e3 = (EditText) findViewById(R.id.orgname);
e4 = (EditText) findViewById(R.id.address);
e5 = (EditText) findViewById(R.id.city);
e6 = (EditText) findViewById(R.id.state);
e7 = (EditText) findViewById(R.id.zip);
nextClick();
}
public void nextClick()
{
final Context context = this;
Button next =(Button) findViewById(R.id.userNext);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
FileOutputStream fos = openFileOutput("data.txt", Context.MODE_WORLD_WRITEABLE);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write(e1.getText().toString());
fos.flush();
fos.close();
}
catch(Exception e) {
}
try
{
FileInputStream fis = new FileInputStream("data.txt");
while(fis.read()!=-1)
{
System.out.println((char)fis.read());
}
}
catch(Exception e)
{
}
Intent intent3 = new Intent(context,com.example.cheruvu.rs.user2.class);
startActivity(intent3);
}
});
}
}