微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在从片段调用的BottomSheet中使用setOnEditorActionListener?

如何解决如何在从片段调用的BottomSheet中使用setOnEditorActionListener?

我目前正在使用带有Kotlin的android-studio开发应用程序。

当从setonEditorActionListener调用BottomSheet中的Fragment触发时,我想使用我的函数

但是在我的代码setonEditorActionListener不会触发。

*我想在CountSettingFragment.kt下面添加评论的地方使用我的函数

如何解决此问题?


以下是代码

build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.sample.myapp"
        minSdkVersion 16
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation filetree(dir: 'libs',include: ['*.jar'])
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-beta01'
    implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

    implementation "com.google.android.material:material:1.1.0-alpha06"

    def nav_version = '2.0.0'
    implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
    implementation "androidx.navigation:navigation-ui-ktx:$nav_version"

    implementation "androidx.fragment:fragment-ktx:1.3.0-alpha04"

    implementation 'com.google.android.material:material:1.0.0'

}

MainActivity.kt

package com.sample.myapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController
import androidx.navigation.ui.NavigationUI.setupWithNavController
import com.sample.myapp.fragments.simpledialogFragment
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val navController = findNavController(R.id.nav_host_fragment)
        setupWithNavController(bottom_navigation,navController)

    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
>
<fragment
        android:id="@+id/nav_host_fragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/navigation_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        app:menu="@menu/bottom_navigation_menu"/>
</LinearLayout>

bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
            android:id="@+id/navi_history"
            android:title="test"
    />
   
</menu>

navigation_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/navigation_graph"
            app:startDestination="@id/navi_history"
>

<fragment android:id="@+id/navi_history"
          android:name="com.sample.myapp.fragments.HistoryFragment"
          android:label="HistoryFragment"/>

</navigation>

HistoryFragment.kt

package com.sample.myapp.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.sample.myapp.R

class HistoryFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_history,container,false)

        val startCount = view.findViewById<Button>(R.id.startCount)

        startCount.setonClickListener{
            val fm = activity!!.supportFragmentManager
            val bottomSheet = CountSettingFragment()
            bottomSheet.show(fm,"navigation_bottom_sheet")
        }

        return view
    }
}

fragment_history.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/constraintLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragments.HistoryFragment"
>
    <Button
            android:id="@+id/startCount"
            android:text="bottom sheet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginTop="8dp"
            app:layout_constraintTop_toTopOf="parent" android:layout_marginBottom="8dp"
            app:layout_constraintBottom_toBottomOf="parent"
    />

</androidx.constraintlayout.widget.ConstraintLayout>

CountSettingFragment.kt

package com.sample.myapp.fragments

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import com.sample.myapp.R

class CountSettingFragment : BottomSheetDialogFragment() {

    override fun onCreateView(
        inflater: LayoutInflater,savedInstanceState: Bundle?
    ): View? {

        val view = inflater.inflate(R.layout.fragment_count_setting,false)

        val editTextinitialNumber = view.findViewById<EditText>(R.id.editTextinitialNumber)

        editTextinitialNumber.setonEditorActionListener { v,actionId,event ->
            when (actionId) {
                EditorInfo.IME_ACTION_DONE -> {
                    print("checked")
                    // I want use my function here
                    true
                }
                else -> false
            }
        }
        return view
    }
}

fragment_count_setting.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".fragments.CountSettingFragment"
>

    <EditText
            android:id="@+id/editTextinitialNumber"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:ems="10"
            android:imeOptions="actionDone"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginTop="32dp"
            app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Android Studio:3.3.2

解决方法

我无法检查功能的原因是,当我将print方法更改为println方法时,可以使用该功能了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。