我想在对话框出现时单击“继续”按钮显示我的文本文件。这是我做了多远。当我点击"继续"按钮它不显示对话框中的任何文本。 我的文本文件保存在桌面上。
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void OnLoad(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Please read carefully!");
builder.setIcon(R.drawable.news);
builder.setCancelable(false);
builder.setPositiveButton("Yes", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
try {
FileInputStream fin =openFileInput("bloodline.txt");
InputStreamReader insr = new InputStreamReader(fin);
int i = 0;
String str = "";
while ((i = insr.read()) != -1) {
str = str + (char) i;
}
Toast.makeText(getBaseContext(), " " + str,
Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
System.out.println("File Not available " + e.getMessage());
Toast.makeText(getBaseContext(),
"File Not available " + e.getMessage(), Toast.LENGTH_SHORT)
.show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(getBaseContext(), "IO Exception " + e.getMessage(),
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
);
builder.setNegativeButton("Declined", new OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getBaseContext(), "You have declined",
Toast.LENGTH_SHORT).show();
}
});
}
}
提前致谢。