Kotlin 中是否有类似的东西可以提供与 Swift 关键字“defer”相同的功能?
defer 关键字的作用是确保 defer 块内的代码在从函数返回之前得到执行。
下面是一个假设 Kotlin 中存在 defer 关键字的示例。
class MyClass {
var timeStamp = 0L
fun isEdible(fruit: Fruit): Boolean {
defer {
timeStamp = System.currentTimeMillis()
}
if (fruit.isExpired) {
return false
}
if (fruit.isRipe) {
return true
}
return false
}
}
Run Code Online (Sandbox Code Playgroud)
在上面的情况下,无论函数在什么时候返回,内部的块都defer将被执行,并且时间戳的值将在函数结束之前更新。
我知道 Java 有finally {}与 一起使用的关键字try{} catch{},但它并不完全是 defer 提供的。
我正在使用android webview,在这里我坚持使用视频.实际上我正在尝试播放资产文件夹但不播放的视频.在这里,我已经完成了所有的链接和解决方案,但没有什么对我有用 - 比如
和
[link2] [2] http://www.mocoven.com/blog/?p=199
以下我附上代码 -
// Html5webview
package com.example.jbb_video_play;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class HTML5WebView extends WebView {
private Context mContext;
private MyWebChromeClient mWebChromeClient;
private View mCustomView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private FrameLayout mContentView;
private FrameLayout mBrowserFrameLayout; …Run Code Online (Sandbox Code Playgroud) 我有一个我想要运行的定期工作,它是在Evernote的Android Job库的帮助下实现的.
我希望实现的是将myMyLocation更新为15分钟.
问题是,每隔15分钟,这项工作似乎要多次执行.
我使用OnePlus3设备进行了测试,并且通过调试,我观察到
LocationUpdateJob.schedule()只调用一次,这是正确的,但是LocationUpdateJob.onRunJob()多次调用,这是不正确的,但应该每15分钟调用一次.
此外,根据崩溃分析,一些设备会抛出illegalStateExceptions.此特殊例外仅在Android 7设备上发生.
以下是崩溃报告中的崩溃:
Fatal Exception: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.myapp/MainActivity}: java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2947)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3008)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1650)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6688)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1468)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1358)
Caused by java.lang.IllegalStateException: Apps may not schedule more than 100 distinct jobs
at android.os.Parcel.readException(Parcel.java:1701)
at android.os.Parcel.readException(Parcel.java:1646)
at android.app.job.IJobScheduler$Stub$Proxy.schedule(IJobScheduler.java:158)
at android.app.JobSchedulerImpl.schedule(JobSchedulerImpl.java:42)
at com.evernote.android.job.v21.JobProxy21.schedule(SourceFile:198)
at com.evernote.android.job.v21.JobProxy21.plantPeriodic(SourceFile:92)
at com.evernote.android.job.JobManager.scheduleWithApi(SourceFile:282)
at com.evernote.android.job.JobManager.schedule(SourceFile:240)
at com.evernote.android.job.JobRequest.schedule(SourceFile:366) …Run Code Online (Sandbox Code Playgroud) 我正在使用Firebase制作聊天应用程序,基本上该应用程序运行良好。我正在使用UI,它存在一个问题,即消息接收者或来自发送者的消息是RecyclerView的一侧,我希望它的两面(右边是发送者,左边是接收者)我有2个XML文件用于消息接收者和发送者已经如此,我如何将send_message_input.xml用于发件人,将item_message.xml用于收件人(对不起,我不知道如何清楚地描述我的问题)。
这是我的代码:
MainActivity.java
public class MainActivity extends AppCompatActivity
implements GoogleApiClient.OnConnectionFailedListener {
public static class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageTextView;
public TextView messengerTextView;
public CircleImageView messengerImageView;
public MessageViewHolder(View v) {
super(v);
messageTextView = (TextView) itemView.findViewById(R.id.messageTextView);
messengerTextView = (TextView) itemView.findViewById(R.id.messengerTextView);
messengerImageView = (CircleImageView) itemView.findViewById(R.id.messengerImageView);
}
}
private static final String TAG = "MainActivity";
public static final String MESSAGES_CHILD = "messages";
private static final int REQUEST_INVITE = 1;
public static final int DEFAULT_MSG_LENGTH_LIMIT = 150;
public static final String ANONYMOUS = …Run Code Online (Sandbox Code Playgroud) 我有一个TextInputLayout需要大量重用的自定义样式,因此我尝试将其转换为自定义视图。
这是要重用的 xml:
<com.google.android.material.textfield.TextInputLayout
style="@style/TextInputLayoutAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
TextInputLayoutAppearance是我在中创建的自定义样式styles.xml
这是我的自定义视图的类:
class OutlinedTextInput @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextInputLayout(context, attrs, defStyleAttr) {
init {
LayoutInflater.from(context).inflate(R.layout.view_outlined_textinput, this, true)
}
}
Run Code Online (Sandbox Code Playgroud)
这是view_outlined_textinput我根据上面的原始 xml 改编的,用于自定义视图:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:parentTag="com.google.android.material.textfield.TextInputLayout">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</merge>
Run Code Online (Sandbox Code Playgroud)
这里有两件事需要注意:
布局使用合并标签来避免视图层次结构中的冗余视图。
应用自定义样式至关重要。使用 style= 语法在原始 xml 中应用样式。但是,由于合并标签用于自定义视图,因此无法以这种方式完成。
我尝试按如下方式设置样式,但没有成功:
class OutlinedTextInput @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = …Run Code Online (Sandbox Code Playgroud) android-custom-view android-layout android-styles android-textinputlayout android-textinputedittext
我正在使用daimajia/AndroidImageSlider库,我需要在没有PageIndicator的情况下显示我的图像.
我试过了 :
mImageSlider.setIndicatorVisibility(PagerIndicator.IndicatorVisibility.Invisible);
这消除了指示器中的点,然而,仍然保留了点所在的灰色矩形形状.
我需要摆脱灰色矩形区域,有解决方案吗?
我正在尝试使用新的Android 计费客户端库 (1.0)
以前,在尝试执行购买时,可以选择向意图添加额外数据。
但是,在使用新库时,事情已经在很大程度上简化了。但是有没有办法将开发人员有效负载(额外字符串)添加到购买流程中?