Android N版本获取根目录下文件的方法。答对追加最高分!

2025-04-03 20:32:10
推荐回答(1个)
回答1:

你还可以,@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText keywordText = (EditText)this.findViewById(R.id.keyword);
Button button = (Button)this.findViewById(R.id.button);
TextView result = (TextView)this.findViewById(R.id.result);
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
String keyword = keywordText.getText().toString();
if (keyword.equals("")) {
result.setText("请勿输入空白的关键词!!");
}else {
result.setText(searchFile(keyword));
}
}
});
}

private String searchFile(String keyword) {
String result = "";
File[] files = new File("/").listFiles();
for (File file : files) {
if (fike.getName().indexOf(keyword) >= 0) {
result += file.getPath() + "\n";
}
}
if (result.equals("")){
result = "找不到文件!!";
}
return result;
}