小编ear*_*jim的帖子

Android:从字符串值中提取链接

我想要来自共享意图的链接。当我通过 Chrome 收到链接时,其格式正确,但有时其他应用程序也会添加文本。

例子:

Chrome:“www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play”

Twitter: "大家看看这个链接,它太酷了https://www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play "

因此,如果是 Twitter,我想删除所有上下文,只保留链接,即 www.recode.net/2016/7/21/12243560/google-machine-learning-comics-play

注意:链接可以是任何格式 https://..(或)www. ..(或)recode.net/...(开头不带 www)。

有正则表达式可以解决这个问题吗?

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shareintent);

    // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) 
    {
        if ("text/plain".equals(type)) 
        {
            // Handle text being sent
            handleSendText(intent); 
        }
    }
}

void handleSendText(Intent intent)
{
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) 
    {
        // Update UI to …
Run Code Online (Sandbox Code Playgroud)

java regex android

1
推荐指数
1
解决办法
3190
查看次数

如何禁用 Kotlin 中的某些 PopupMenu 项目?

我使用以下代码 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)

android kotlin

1
推荐指数
1
解决办法
1477
查看次数

Kotlin中的字符串值

在Java中,如果我们new String() 知道它会创建新的字符串对象,它将与没有'new'的对象不同(即使内容相同).

//Java    
System.out.println("First" == new String("First")); // false always
Run Code Online (Sandbox Code Playgroud)

在Kotlin中,如果我尝试创建String甚至创建StringBuilder,它仍然与没有String(..)创建的相同.

//Kotlin
println("First" == String(StringBuilder("First"))) //true always
Run Code Online (Sandbox Code Playgroud)

如果创建String(StringBuilder(..))的将重用相同的字符串值,为什么要给构造函数?是否有任何增值,寻找这样的用例.

谢谢.

kotlin

1
推荐指数
2
解决办法
504
查看次数

BroadcastReceiver 未接收本地广播

我正在尝试扩展 FCM 服务,并在被呼叫MainActivity时广播。确实被调用了,但是方法没有被调用。据我所知,我不需要在清单中定义任何其他内容,因为这是本地广播。这些是我的课程:onNewToken()onNewToken()onReceive()

MainActivity.kt

package com.reali.app.mymessagingapp

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.LocalBroadcastManager
import android.widget.TextView
import com.google.android.gms.tasks.OnCompleteListener
import com.reali.app.mymessagingapp.MyFirebaseMessagingService.Companion.TOKEN_REFRESHED_EVENT
import com.google.firebase.iid.FirebaseInstanceId
import com.google.firebase.iid.InstanceIdResult

class MainActivity : AppCompatActivity() {
    private lateinit var broadcastReceiver: BroadcastReceiver
    private lateinit var tvTitle: TextView
    private lateinit var tvToken: TextView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        tvTitle = findViewById(R.id.tvTitle)
        tvToken = findViewById(R.id.tvToken)

        broadcastReceiver = object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) …
Run Code Online (Sandbox Code Playgroud)

android kotlin

1
推荐指数
1
解决办法
2647
查看次数

如何检查imageview图像是否未更改

我有这样的问题ImageView,默认情况下我设置ivImage.setImageResource(R.drawable.avatar) 当我点击"保存"按钮,如果图像输入ImageView没有改变,它不应该保存图像.我尝试过这个,但它不起作用

private Drawable oldDrawable;
Run Code Online (Sandbox Code Playgroud)

在onCreate()

oldDrawable = imgAvatarDoctor.getDrawable();
Run Code Online (Sandbox Code Playgroud)

并在按钮单击

 if (imgAvatarDoctor.getDrawable() == oldDrawable) {
            isNoError = false;
        }
Run Code Online (Sandbox Code Playgroud)

那么我该如何解决这个问题呢?非常感谢

android imageview

0
推荐指数
1
解决办法
939
查看次数

Kotlin相当于检查是否为空

以下代码段可以使用!?运营商在Kotlin中以较短的形式编写:

val acct: GoogleSignInAccount?  = result.signInAccount
if (acct != null && acct.displayName != null)
    MagicToast.showSuccess(this, "Account Name: " + acct.displayName)
Run Code Online (Sandbox Code Playgroud)

android kotlin

-3
推荐指数
1
解决办法
112
查看次数

标签 统计

android ×5

kotlin ×4

imageview ×1

java ×1

regex ×1