Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 这个带问号的函数在做什么?_Android - Fatal编程技术网

Android 这个带问号的函数在做什么?

Android 这个带问号的函数在做什么?,android,Android,这个函数在getAction()之后做什么特别的问号 如果操作不为null或空,则返回操作的意图,否则返回空字符串 也可以这样写 String strAction; if(getIntent().getAction() == null || getIntent().getAction().isEmpty()) { strAction = ""; } else { strAction = getIntent().getAction(); } 语法?:称为 你的代码行 String s

这个函数在getAction()之后做什么特别的问号


如果操作不为null或空,则返回操作的意图,否则返回空字符串

也可以这样写

String strAction;
if(getIntent().getAction() == null || getIntent().getAction().isEmpty()) {
   strAction = "";
} else {
   strAction = getIntent().getAction();
}

语法
?:
称为

你的代码行

String strAction = !MyRUtils.isNullOrEmpty(getIntent().getAction()) 
         ? getIntent() .getAction() 
         : "";
相当于:

String strAction = null;
if (!MyRUtils.isNullOrEmpty(getIntent().getAction()) {
     strAction = getIntent().getAction();
} else {
     strAction = "";
}
正如您所看到的,它只是一些语法上的糖分,可以用来避免冗长的if-else块

String strAction = null;
if (!MyRUtils.isNullOrEmpty(getIntent().getAction()) {
     strAction = getIntent().getAction();
} else {
     strAction = "";
}