在此代码中,仅读取.txt文件中输入的最后一个单词。当条件满足时,我正在调用相同的活动。我应该对代码做出哪些更改,以便它读取一个单词,再次调用相同的活动,然后读取一个新单词?
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str2 = e.getText().toString();
if (str2.equals(x)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
您可以使用此代码逐行读取文件
try {
FileReader fileReader = new FileReader("FilePath");
BufferedReader reader = new BufferedReader(fileReader);
String data = null;
StringBuilder builder = new StringBuilder();
while ((data = reader.readLine()) != null) {
builder.append(data);
}
System.out.println(builder.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
在你的代码中你的onclick监听器有问题..请把onclick监听器放在while循环中..
答案 1 :(得分:1)
onClickListener()中存在minot错误:
public class Page extends Activity {
Button b;
TextView t;
EditText e;
int i = 0;
String str2;
String w, x=null;
static BufferedReader c = null;
static BufferedReader ic = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
b = (Button) findViewById(R.id.benter);
t = (TextView) findViewById(R.id.tv);
e = (EditText) findViewById(R.id.et);
InputStream f = getResources().openRawResource(R.raw.incorrect);
ic = new BufferedReader(new InputStreamReader(f));
try {
while ((w = ic.readLine()) != null) {
t.setText(w);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
str2 = e.getText().toString();
// it was str2.equals(x) nut x is null, replace w with x
if (str2.equals(w)) {
Toast.makeText(getApplication(), "RIGHT", Toast.LENGTH_LONG).show();
Intent open = new Intent(Page.this, Page.class);
startActivity(open);
} else {
Toast.makeText(getApplication(), "WRONG", Toast.LENGTH_LONG).show();
e.setText("");
}
}
});
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
此代码适用于您
答案 2 :(得分:0)
你可以使用Apache Commons I / O库。这很简单。并保持你的代码美。为什么从资产中获取.txt文件?
AssetManager am = context.getAssets();
InputStream fs = am.open("a.txt");
List<String> lines = IOUtils.readLines(file,"UTF-8");
答案 3 :(得分:0)
您可以将文本文件保存在&#34;资产&#34;项目文件夹,并使用以下代码在java类中检索该文件
try {
reader = new BufferedReader(
new InputStreamReader(getAssets().open("YOUR_TEXT_FILE.txt")));
StringBuilder total = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
total.append(line);
}
message=total.toString();
System.out.println(message);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
之后,您将在String消息中包含该文件
答案 4 :(得分:0)
您的计划正在阅读最后一行,因为您正在覆盖您的&#39; w&#39;每次调用readLine()方法时都会变量。你应该做的是设置你的&#39; w&#39;变量为空, 追加 与readLine()一起保留文本文件中已读取的所有行,然后调用setText()一次将所有内容放在View中。没有附加,你基本上覆盖了&#39; w&#39;每次while循环运行时变量,这就是你从文本文件接收最后一行的原因。