Android - 检查文件扩展名是否为 .mp4
Android - Check if file extension is .mp4
我想检查用户是否输入了扩展名 .mp4
以及输入是否正确。我知道我可以做这样的事情:
if (Rename[0].indexOf(".") > 0) {
}
但问题在于它只是检查输入的字符是否超过一个,因此如果输入了 .mp5
或任何不正确的扩展名,它将被接受。
这里是我想使用它的上下文:
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Rename[0] = editText.getEditableText().toString();
if (Rename[0] != null){
//This is where I want to check if the extension has been correctly entered
}
}
所以我的问题是:
如何从Rename{0}
中获取文本,这个已经搞定了,检查文件扩展名.mp4
是否输入正确,如果没有,则添加,否则如果输入(正确)忽略它?
试试这个
if (Rename[0].endsWith(".mp4")) {
}
通过以下代码执行此操作:
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Rename[0] = editText.getEditableText().toString();
if (Rename[0] != null){
if(Rename[0].contains(".mp4"){
//do what you want to do here
}
}
}
我想检查用户是否输入了扩展名 .mp4
以及输入是否正确。我知道我可以做这样的事情:
if (Rename[0].indexOf(".") > 0) {
}
但问题在于它只是检查输入的字符是否超过一个,因此如果输入了 .mp5
或任何不正确的扩展名,它将被接受。
这里是我想使用它的上下文:
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Rename[0] = editText.getEditableText().toString();
if (Rename[0] != null){
//This is where I want to check if the extension has been correctly entered
}
}
所以我的问题是:
如何从Rename{0}
中获取文本,这个已经搞定了,检查文件扩展名.mp4
是否输入正确,如果没有,则添加,否则如果输入(正确)忽略它?
试试这个
if (Rename[0].endsWith(".mp4")) {
}
通过以下代码执行此操作:
alertDialogBuilder.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Rename[0] = editText.getEditableText().toString();
if (Rename[0] != null){
if(Rename[0].contains(".mp4"){
//do what you want to do here
}
}
}