如何通过单击按钮清除存储的Edittext值(存储文件)?

时间:2017-07-20 10:38:00

标签: java android

让我解释一下(我的英语很糟糕,对不起)。我创建了一个活动,其中有三个Edittext框要填充。在填满之后,有一个"提交"将数据传递给另一个活动的按钮,以便稍后在各自的文本视图中显示。问题是,每当我填写这些edittexts并传递信息时,它就会收集之前传递的数据。我有一个明确的'按钮,但这只有助于清除文本视图显示。我想让它清除存储的数据,以便下次再次传递数据时,textviews显示新传递的数据,而不是与之前的数据混合。

下面的图片示例:

[https://i.stack.imgur.com/txv81.png][1]

我想确保当我下次输入editexts时,活动应该只显示那个时刻键入的内容,并且清楚地显示'清除'按钮删除所有先前存储的数据。

这里是FillInOwnStory代码(填写此活动中的edittexts)

package com.example.msi.astorydrivengame;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import org.w3c.dom.Text;

import java.io.FileOutputStream;


public class FillInOwnStory extends AppCompatActivity {


Button storysubmit;
Button menubutton2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fill_in_own_story);

    fillStoryName = (EditText) findViewById(R.id.fillStoryName);
    fillAuthor = (EditText) findViewById(R.id.fillAuthor);
    fillStoryDescp = (EditText) findViewById(R.id.fillStoryDescp);
    storysubmit = (Button) findViewById(R.id.storysubmit);
    menubutton2 = (Button) findViewById(R.id.menubutton2);
}

private EditText fillStoryName;
private EditText fillAuthor;
private EditText fillStoryDescp;
private TextView StoryNameColumn;
private TextView AuthorColumn;
private TextView DescpColumn;


public void buttonClickFunction26(View view) {
    fillStoryName = (EditText) findViewById(R.id.fillStoryName);
    String StoryNameFilled = fillStoryName.getText().toString();
    fillAuthor = (EditText) findViewById(R.id.fillAuthor);
    String AuthorNameFilled = fillAuthor.getText().toString();
    fillStoryDescp = (EditText) findViewById(R.id.fillStoryDescp);
    String DescpFilled = fillStoryDescp.getText().toString();

    try{
        FileOutputStream out = openFileOutput("storyname.txt", Context.MODE_APPEND);
        out.write(StoryNameFilled.getBytes());
        out.close();
    }catch(java.io.IOException e){
        e.printStackTrace();
    }
    try{
        FileOutputStream out = openFileOutput("author.txt", Context.MODE_APPEND);
        out.write(AuthorNameFilled.getBytes());
        out.close();
    }catch(java.io.IOException e){
        e.printStackTrace();
    }
    try{
        FileOutputStream out = openFileOutput("descp.txt", Context.MODE_APPEND);
        out.write(DescpFilled.getBytes());
        out.close();
    }catch(java.io.IOException e){
        e.printStackTrace();
    }

    Intent intent = new Intent(this, FillColumn.class);
    intent.putExtra("storyname_label", StoryNameFilled);
    intent.putExtra("author_label", AuthorNameFilled);
    intent.putExtra("descp_label", DescpFilled);
    startActivity(intent);


}
}

这里是FillColumn代码(edittext值显示在给定的文本视图中。)

package com.example.msi.astorydrivengame;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;



public class FillColumn extends AppCompatActivity {

TextView StoryNameColumn;
TextView AuthorColumn;
TextView DescpColumn;
Button menubutton3;
Button clearbutton;
String xxx;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fill_column);

    StoryNameColumn = (TextView) findViewById(R.id.StoryNameColumn);
    AuthorColumn = (TextView) findViewById(R.id.AuthorColumn);
    DescpColumn = (TextView) findViewById(R.id.DescpColumn);
    menubutton3 = (Button) findViewById(R.id.menubutton3);
    clearbutton = (Button) findViewById(R.id.clearbutton);


    Intent intentx = getIntent();
    StoryNameColumn.setText(intentx.getStringExtra("storyname_label"));
    try {
        InputStream instream = openFileInput("storyname.txt");

        if (instream != null) {
            InputStreamReader inputreader = new InputStreamReader(instream);
            BufferedReader buffreader = new BufferedReader(inputreader);

            String line;

            while ((line = buffreader.readLine()) != null) {
                StoryNameColumn.setText(line);
            }
        }
        instream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    AuthorColumn.setText(intentx.getStringExtra("author_label"));
    try {
        InputStream instream = openFileInput("author.txt");

        if (instream != null) {
            InputStreamReader inputreader = new InputStreamReader(instream);
            BufferedReader buffreader = new BufferedReader(inputreader);

            String line;

            while ((line = buffreader.readLine()) != null) {
                AuthorColumn.setText(line);
            }
        }
        instream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    DescpColumn.setText(intentx.getStringExtra("descp_label"));
    try {
        InputStream instream = openFileInput("descp.txt");

        if (instream != null) {
            InputStreamReader inputreader = new InputStreamReader(instream);
            BufferedReader buffreader = new BufferedReader(inputreader);

            String line;

            while ((line = buffreader.readLine()) != null) {
                DescpColumn.setText(line);
            }
        }
        instream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

}


public void buttonClickFunction30(View view) {

    Intent intent = new Intent(getApplicationContext(), StoryPageChoices.class);
    String strNAME = "Slot 1";
    intent.putExtra("NewStory", strNAME);
    startActivity(intent);

}

public void clear(View v) {
    StoryNameColumn.setText("");
    AuthorColumn.setText("");
    DescpColumn.setText("");

}
}

我知道我的编码不干净而且搞砸了,我的英语很糟糕,对不起。

2 个答案:

答案 0 :(得分:0)

只需在EditText

中将文字设为空字符串即可
onClick(View view){
    ----
    yourEditText.getText().clear();  
    //or
    yourEditText.setText("");
}

答案 1 :(得分:0)

这可能是因为你正在做的事情:

此行可根据您的需要设置上一个活动的文字:

StoryNameColumn.setText(intentx.getStringExtra("storyname_label"));

但在该行之后,您正在使用此代码覆盖您在StoryNameColumn中设置的值。尝试删除从文件中读取的所有行。

try {
    InputStream instream = openFileInput("storyname.txt");

    if (instream != null) {
        InputStreamReader inputreader = new InputStreamReader(instream);
        BufferedReader buffreader = new BufferedReader(inputreader);

        String line;

        while ((line = buffreader.readLine()) != null) {
            StoryNameColumn.setText(line);
        }
    }
    instream.close();
} catch (IOException e) {
    e.printStackTrace();
}

已编辑:

public class FillColumn extends AppCompatActivity {

TextView StoryNameColumn;
TextView AuthorColumn;
TextView DescpColumn;
Button menubutton3;
Button clearbutton;
String xxx;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fill_column);

StoryNameColumn = (TextView) findViewById(R.id.StoryNameColumn);
AuthorColumn = (TextView) findViewById(R.id.AuthorColumn);
DescpColumn = (TextView) findViewById(R.id.DescpColumn);
menubutton3 = (Button) findViewById(R.id.menubutton3);
clearbutton = (Button) findViewById(R.id.clearbutton);


Intent intentx = getIntent();
StoryNameColumn.setText(intentx.getStringExtra("storyname_label"));
AuthorColumn.setText(intentx.getStringExtra("author_label"));
DescpColumn.setText(intentx.getStringExtra("descp_label"));
}


public void buttonClickFunction30(View view) {

Intent intent = new Intent(getApplicationContext(), StoryPageChoices.class);
String strNAME = "Slot 1";
intent.putExtra("NewStory", strNAME);
startActivity(intent);

}

public void clear(View v) {
StoryNameColumn.setText("");
AuthorColumn.setText("");
DescpColumn.setText("");

  }
}