我想将 java 的代码转换为 Kotlin:
private static void appendHex(StringBuffer sb, byte b) {
sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
}
Run Code Online (Sandbox Code Playgroud)
我得到:
private fun appendHex(sb: StringBuffer, b: Byte) {
sb.append(hex.toCharArray()[b shr 4 and 0x0f]).append(hex.toCharArray()[b and 0x0f])
}
Run Code Online (Sandbox Code Playgroud)
但是 Kotlin 的标准shr期望 Int 作为第一个参数(不是Byte)。and运营商同样的问题。
如何将其转换为 Kotlin?
我想HashMap<String?, String?>?从firebaseDatabase:
override fun onDataChange(dataSnapshot: DataSnapshot) {
val users: HashMap<String?, String?>? = dataSnapshot.value as HashMap<String?, String?>? // todo !!!
if (users != null) {
if (!users.containsKey(userUid)) {
users[userUid] = userName
}
}
}
Run Code Online (Sandbox Code Playgroud)
此代码有效,但Android Studio在第二行显示警告:
Unchecked cast: Any? to HashMap<String?, String?>?
Run Code Online (Sandbox Code Playgroud)
如何以正确的方式解决这个问题?
我从这样的 SOAP API 得到结果:
client = zeep.Client(wsdl=self.wsdl, transport=transport)
auth_header = lb.E("authenticate", self.login())
res = client.service.GetHouseProfile(region_id, page_number, reporting_period_id, _soapheaders=[auth_header])
Run Code Online (Sandbox Code Playgroud)
现在我需要解析 res 并得到结果。
>>> dir(res)
['__class__', '__contains__', '__deepcopy__', '__delattr__', '__delitem__', '__dict__', '__doc__', '__eq__', '__format__', '__getattribute__', '__getitem__', '__hash__', '__init__', '__iter__', '__len__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', '__values__', '__weakref__', '_xsd_type']
>>> type(res)
<class 'zeep.objects.GetHouseProfileSFResponse'>
>>> print(res.__str__()[0:100])
{
'data': {
'item': [
{
'house_id': 6465882L,
Run Code Online (Sandbox Code Playgroud)
如何从 res 中获取某个元素?
于是我找到了方法。看起来不是一个标准的决定,但它有效:
>>> res.__values__.get("data").__values__.get("item")[6].__values__.keys()
[u'house_id', u'house_profile_data', u'full_address', u'stage', u'state', u'emergency_date', u'emergency_number', u'emergency_reason', u'emergency_after', …Run Code Online (Sandbox Code Playgroud) 我有一个可组合函数TextField:
val focusManager = LocalFocusManager.current
TextField(
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Search,
),
keyboardActions = KeyboardActions(
onSearch = {
focusManager.clearFocus()
}
)
)
Run Code Online (Sandbox Code Playgroud)
当我单击不属于可组合内容的其他按钮时,我需要从可组合函数内部以及外部显示键盘。基本上我想从我的片段中调用hideKeyboard()。
我尝试在可组合项中使用 livedata:
val shouldShowKeyBoard by shouldShowSearchKeyBoard.observeAsState()
Run Code Online (Sandbox Code Playgroud)
我可以focusManager.clearFocus()隐藏键盘,但我不确定如何以编程方式显示特定的组合TextField
管理隐藏/显示键盘的“撰写”方式是什么?
我有几个图标row
Row {
IconButton {
Icon(
painter = painterResource(R.drawable.im1)
)
},
IconButton {
Icon(
painter = painterResource(R.drawable.im2)
)
}
}
Run Code Online (Sandbox Code Playgroud)
但是当它显示时,两个图标之间的距离比row我预期的要大。我感觉它们之间有 32dp 的间隔。如何减少 2 个图标之间的距离row?
Navigation我在使用带有此代码的组件时加载片段,并且它有效。
findNavController().navigate(R.id.menu_nav_graph, bundleOf("menuItem" to item))
Run Code Online (Sandbox Code Playgroud)
我想在单击按钮时关闭片段,我使用此代码
findNavController().popBackStack()
Run Code Online (Sandbox Code Playgroud)
应用程序导航到上一个片段,但是当我尝试使用上面的代码再次导航到弹出的片段目的地时,应用程序到达代码但没有任何反应。片段未加载。因此,导航代码已执行,但片段未打开。popBackStack当我不使用它与 onClick 监听器一起使用时,也会发生同样的情况
activity?.onBackPressed()
Run Code Online (Sandbox Code Playgroud)
同样的效果,应用程序到达导航线,没有崩溃,没有抛出异常,只是没有打开 Fragment。
同时,我的后退箭头导航正在工作,并且在从片段按回后,它确实多次转到同一目的地。这让我感到困惑,因为我使用相同的代码并且onOptionsItemSelected它可以工作,所以我不明白当我只调用onBackPressed()按钮单击时它有何不同。
override fun onOptionsItemSelected(item: MenuItem): Boolean =
when (item.itemId) {
android.R.id.home -> {
activity?.onBackPressed()
true
}
else -> super.onOptionsItemSelected(item)
}
Run Code Online (Sandbox Code Playgroud)
如何在单击按钮时关闭片段并能够导航到相同的目的地?
navigation android android-fragments android-navigation android-jetpack
我有一个改造服务:
suspend fun getArticles(): Articles
Run Code Online (Sandbox Code Playgroud)
通常我可以try/catch在出现错误的情况下获得响应代码。
try {
val articles = service.getArticles()
} catch (e: Exception) {
// I can get only codes different from 200...
}
Run Code Online (Sandbox Code Playgroud)
但是如果我需要区分 200 和 202 代码并且我的服务只返回数据对象怎么办?
如果响应成功,如何获取响应代码?
我有可组合的空片段:
setContent {
Surface(
modifier = Modifier
.fillMaxWidth().fillMaxHeight().padding(bottom = 48.dp, top = 16.dp),
color = colorResource(id = R.color.usaa_white)
) {
val itemsList = (0..50).toList()
val itemsIndexedList = listOf("A", "B", "C")
LazyColumn(
) {
items(itemsList.size) {
Text("Item is $it")
}
item {
Text("Single item")
}
itemsIndexed(itemsIndexedList) { index, item ->
Text("Item at index $index is $item")
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
问题是:我只能滚动内容直到“单个项目”行,其余内容被隐藏。我添加了一些填充,以确保它不是底部导航栏覆盖列表,但它仍然被裁剪。
我想乘281.65用100和得到28165,我执行:
fun main(args: Array<String>) {
println("${281.65 * 100}")
}
Run Code Online (Sandbox Code Playgroud)
但我得到28164.999999999996
这里的问题是什么,如何得到28165结果?有没有好的Kotlin处理方式?
android ×6
kotlin ×5
java ×1
lazycolumn ×1
navigation ×1
python ×1
retrofit ×1
retrofit2 ×1
soap ×1
zeep ×1