我运行代码A并得到结果A。在我看来,它应该是结果B。
看来flow.collect { value -> println(value) }要执行的块。
Flow 块的收集会执行吗?
代码A
fun simple(): Flow<Int> = flow {
println("Flow started")
for (i in 1..3) {
delay(300)
emit(i)
}
}
fun main() = runBlocking<Unit> {
println("Calling simple function...")
val flow = simple()
println("Calling collect...")
flow.collect { value -> println(value) } //Block?
println("Calling collect again...")
}
Run Code Online (Sandbox Code Playgroud)
结果A
Calling simple function...
Calling collect...
Flow started
1
2
3
Calling collect again...
Run Code Online (Sandbox Code Playgroud)
结果B
Calling simple function...
Calling collect...
Flow started
Calling collect again...
1 …Run Code Online (Sandbox Code Playgroud) 我知道Column(){...}当b1orb2改变时,将会重新组合。
如果我希望只有改变的Column(){...}时候才可以重新组合,而改变的 时候不重新组合,怎么办?b2Column(){...}b1
@Composable
fun ScreenDetail(
mViewMode: SoundViewModel
) {
val b1=mViewMode.a1.collectAsState(initial = 0)
val b2=mViewMode.a2.collectAsState(initial = 0)
Column() {
Text(" ${b1.value} ${b2.value}")
Text(Calendar.getInstance().time.toSeconds())
}
}
fun Date.toSeconds():String{
return SimpleDateFormat("yyyy/MM/dd HH:mm:ss", Locale.US).format(this)
}
class SoundViewModel(): ViewModel() {
var i = 0
val a1: Flow<Int> = flow {
while (true) {
emit(i++)
delay(1000)
}
}
val a2: Flow<Int> = flow {
while (true) {
emit(i)
delay(2000)
}
}
}
Run Code Online (Sandbox Code Playgroud) 我在行的左侧显示文本,并在代码 A 的行右侧显示工具按钮。
当文字很短时没关系,你可以看到图像A,如我所料。
但是当文本很长时,我发现该行右侧的一些工具按钮消失了,您可以看到图像B。
我希望行右边的工具按钮能够一直显示,并且当空间不够的时候能够自动截断左边的文字。
我能怎么做 ?感谢!
代码A
Row(
modifier = Modifier.fillMaxWidth(),
verticalAlignment = Alignment.CenterVertically
) {
Text("This a short text")
//Text("This a very long text text text text text text text text")
Spacer(Modifier.weight(1f))
val iconModifier = Modifier.size(24.dp)
IconButton(
enabled = true,
onClick = {
}
) {
Icon(Icons.Filled.Edit , null, modifier = iconModifier)
}
IconButton(
enabled = true,
onClick = {
}
) {
Icon(Icons.Filled.Delete , null, modifier = iconModifier)
}
IconButton(
enabled = true,
onClick = { …Run Code Online (Sandbox Code Playgroud) 我希望myVideoFile用ASCII降序排序,所以我得到的结果newVideoFile.get(0)是"d",newVideoFile.get(1)是"c",newVideoFile.get(2)是"b",newVideoFile.get(3)是"a".
在java中有一种简单的方法吗?
List<String> myVideoFile=new ArrayList<>();
myVideoFile.add("a");
myVideoFile.add("b");
myVideoFile.add("c");
myVideoFile.add("d");
Run Code Online (Sandbox Code Playgroud) 我已经阅读了如何使用NanoHTTPD在Android上的sdcard上提供文件
return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis)最新的NanoHTTPD 2.3.0不再支持该代码。
我尝试将其替换为return newFixedLengthResponse(fis),但这是不正确的。我怎样才能做到这一点?谢谢!
public class StackOverflowMp3Server extends NanoHTTPD {
public StackOverflowMp3Server() {
super(8089);
}
@Override
public Response serve(String uri, Method method,
Map<String, String> header, Map<String, String> parameters,
Map<String, String> files) {
String answer = "";
FileInputStream fis = null;
try {
fis = new FileInputStream(Environment.getExternalStorageDirectory()
+ "/music/musicfile.mp3");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return new NanoHTTPD.Response(Status.OK, "audio/mpeg", fis);
}
}
Run Code Online (Sandbox Code Playgroud) 我已经阅读了http://mings.in/2017/03/12/Kotlin-Null-Safety.html的猫王运营商
代码A和代码C可以,但是代码B是错误的(“类型不匹配:推断的类型为Int但应为单位”),为什么?
代码A
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val b: String? = "hello"
val l = b?.length ?: -1
}
Run Code Online (Sandbox Code Playgroud)
代码B
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val b: String? = "hello"
val l = b?.length ?: return -1
}
Run Code Online (Sandbox Code Playgroud)
代码C
fun getLength(b: String?): Int {
val l = b?.length ?: return -1
return l
}
Run Code Online (Sandbox Code Playgroud)
顺便说一句,BakaWaii告诉我:“代码A可以,因为-1是表示值的表达式,因此可以将其赋给l。但是在代码B中,return -1是从函数返回的表达式。”
我不明白“在代码B中,return -1是从函数返回的表达式”。我认为“ return -1”将返回-1,为什么应用程序会发生“类型不匹配:推断的类型为Int但预期单位”?
和更多:
代码C和代码B一样,我不知道为什么还可以!
我希望使用return而不是let,所以我写了 Code B,但是 Code B 无法编译,出现两个错误,为什么?
错误:(30, 10) 此处不允许“返回”
错误:(30, 45) 类型不匹配:推断的类型是 CityForecast?但预计 CityForecast
代码 A
override fun requestForecastByZipCode(zipCode: Long, date: Long) = forecastDbHelper.use {
val dailyRequest = "${DayForecastTable.CITY_ID} = ? AND ${DayForecastTable.DATE} >= ?"
val dailyForecast = select(DayForecastTable.NAME)
.whereSimple(dailyRequest, zipCode.toString(), date.toString())
.parseList { DayForecast(HashMap(it)) }
val city = select(CityForecastTable.NAME)
.whereSimple("${CityForecastTable.ID} = ?", zipCode.toString())
.parseOpt { CityForecast(HashMap(it), dailyForecast) }
city?.let { dataMapper.convertToDomain(it) }
}
Run Code Online (Sandbox Code Playgroud)
代码 B
override fun requestForecastByZipCode(zipCode: Long, date: Long) …Run Code Online (Sandbox Code Playgroud) 我将mContext定义为val,我需要在有趣的onCreate中为其分配一个值。
代码private lateinit val mContext: Context不正确,我该怎么办?
class UIMain : AppCompatActivity() {
private val mContext: Context
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.layout_main)
mContext = this
}
}
Run Code Online (Sandbox Code Playgroud)
回答Strelok
this就像下面的代码一样,关键字并不总是合适的,因此我认为将其分配this给mContext 更方便。
private Context mContext;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main);
mContext=this;
findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Hello A", Toast.LENGTH_LONG).show();
Toast.makeText(mContext, "Hello B", Toast.LENGTH_LONG).show();
//Toast.makeText(this, "Hello C", Toast.LENGTH_LONG).show(); //Doesn't work
finish();
}
});
}
Run Code Online (Sandbox Code Playgroud) 我使用以下代码 B 单击按钮来打开 PopupMenu.
包括PopupMenu编辑、删除和设置项。
我希望编辑和删除项在没有记录时被禁用或消失,也许就像代码A,我该如何编写代码?
代码A
popup.setOnMenuItemBeforePopup {
if (recordCount==0){
R.id.popMenuMoreEdit.disable
R.id.popMenuMoreDelete.disbale
}
}
Run Code Online (Sandbox Code Playgroud)
代码B
private fun setControls(){
btnMore.setOnClickListener (View.OnClickListener { v -> showPopup(v, mContext) })
}
fun showPopup(v: View, mContext: Context) {
val popup = PopupMenu(mContext, v)
popup.inflate(R.menu.menu_more)
popup.setOnMenuItemClickListener {
item -> handleMenu(item, mContext)
}
popup.show()
}
private fun handleMenu(item: MenuItem, mContext: Context): Boolean {
when (item.itemId) {
R.id.popMenuMoreEdit -> {
return true
}
R.id.popMenuMoreDelete -> {
return true
}
R.id.popMenuMoreBackupSetting-> {
return true
} …Run Code Online (Sandbox Code Playgroud) 我希望监视一个变量,当变量发生变化时我会做一些事情,也许就像代码A一样.
如何在Kotlin中编写这些代码?谢谢!
代码A.
var myList: List<Int>
registerMonitorVar(myList)
fun onVariableChange(){
if (myList.size>=1){
btnDelete.enabled=true
}
}
Run Code Online (Sandbox Code Playgroud)
要ice1000
谢谢!但以下代码不起作用!我不知道如何在需要set属性时初始化allList.
private lateinit var allList: MutableList<MSetting> set(value) {
field = value
onVariableChange()
}
private var allList=mutableListOf<MSetting>() set(value) {
field = value
onVariableChange()
}
fun onVariableChange(){
if (allList.size>=1){
}
}
Run Code Online (Sandbox Code Playgroud)
为了humazed:
谢谢!为什么以下代码不正确?
private var allList: MutableList<MSetting> by Delegates.vetoable(mutableListOf<MSetting>())
{ property, oldValue, newValue ->
{
btnRestore.isEnabled=(newValue.size >= 1)
btnBackup.isEnabled=(newValue.size >= 1)
}
}
Run Code Online (Sandbox Code Playgroud)
要humazed和ice1000
谢谢!当我使用Code 2时,系统无法监视var allList的更改
private var allList: MutableList<MSetting> by Delegates.observable(mutableListOf<MSetting>())
{ property, oldValue, newValue …Run Code Online (Sandbox Code Playgroud)