TextView onClickListener无法识别点击

时间:2019-05-01 22:26:22

标签: android android-activity kotlin textview

基本上,我有一个带有多行输入类型和可点击TextView的EditTex的布局。当我单击TextView时出现问题,似乎setOnClcickListener无法正常工作。

代码调试器显示侦听器无法识别点击。

活动布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <EditText
        android:id="@+id/reviewText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:gravity="start"
        android:inputType="textPassword"
        android:textAlignment="textStart"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.069" />

    <TextView
        android:id="@+id/submitReviewTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:clickable="true"
        android:focusable="true"
        android:text="Enviar Crítica"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.974"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/reviewText"
        app:layout_constraintVertical_bias="0.575" />

</android.support.constraint.ConstraintLayout>

活动代码:

package com.example.myapplication

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private var reviewConditionsOk = true

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


        submitReviewTextView.setOnClickListener {
            checkMandatoryReviewConditions()
            if (!reviewText.text.isEmpty() && !reviewConditionsOk) {
                Log.d("Test", "Button Clicked")
                Toast.makeText(applicationContext,
                    "La crítica no a de estar vacía y tiene que tener un mínimo de 5 líneas.",
                    Toast.LENGTH_LONG
                ).show()
            }
        }
    }

    override fun onBackPressed() {
        super.onBackPressed()
        finish()
    }

    private fun checkMandatoryReviewConditions() {
        reviewText.onFocusChangeListener = View.OnFocusChangeListener { _, hasFocus ->
            if (hasFocus) {
                reviewConditionsOk = reviewText.text.toString().trim().length < 5
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我自己对代码进行了测试,发现点击监听器没有问题。该事件已正确触发。您需要修改以下条件,以使其显示您要获取的public class teamsnews extends AppCompatActivity { private Toolbar toolbar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_teamsnews); WebView htmlWebView = (WebView)findViewById(R.id.webView); // htmlWebView.setWebViewClient(new CustomWebViewClient()); WebSettings webSetting = htmlWebView.getSettings(); webSetting.setJavaScriptEnabled(true); webSetting.setDisplayZoomControls(true); htmlWebView.loadUrl("https://www.yallakora.com/Match-Center/%D9%85%D8%B1%D9%83%D8%B2-%D8%A7%D9%84%D9%85%D8%A8%D8%A7%D8%B1%D9%8A%D8%A7%D8%AA#nav-burgermenu"); SetupToolBar(); } private void SetupToolBar() { toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setTitle(""); TextView textView = findViewById(R.id.toolbar_title); textView.setText("الدورى والبطولات"); } } //manifest: <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.otherHand"> <uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <application android:name=".utility.FirebaseOffline" android:allowBackup="true" android:icon="@drawable/logoy" android:label="@string/app_name" android:supportsRtl="false" android:theme="@style/AppTheme" tools:replace="android:supportsRtl"> <activity android:name=".teamsnews" android:parentActivityName=".DetailsActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".Act_tutoriial" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar"/> <activity android:name=".DawraElramadnia" android:launchMode="singleTop" android:parentActivityName=".DetailsActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar"/> <activity android:name=".SplashScreen" android:launchMode="singleTop" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <!-- > for notification handler<! --> <meta-data android:name="com.onesignal.NotificationOpened.DEFAULT" android:value="DISABLE" /> <activity android:name="com.soundcloud.android.crop.CropImageActivity" /> <activity android:name=".utility.TutorialActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".MainActivity" android:launchMode="singleTop" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".DetailsActivity" android:launchMode="singleTop" android:parentActivityName=".MainActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".CommentActivity" android:parentActivityName=".DetailsActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".ReservationActivity" android:parentActivityName=".DetailsActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".utility.AddStudiam" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".CombineTeam" android:parentActivityName=".DetailsActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> <activity android:name=".GiftsAcivity" android:launchMode="singleTop" android:parentActivityName=".DetailsActivity" android:screenOrientation="portrait" android:theme="@style/AppTheme.NoActionBar" /> </application> </manifest> /// style <resources> <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:windowNoTitle">true</item> <item name="android:windowActionBar">false</item> <item name="android:windowFullscreen">true</item> <item name="android:windowContentOverlay">@null</item> </style> <style name="NoActionBarTheme" parent="Theme.AppCompat.Light.NoActionBar" /> <style name="myRatingBar" parent="@style/Base.Widget.AppCompat.RatingBar.Small"> </style> <style name="CustomRatingBar" parent="@android:style/Widget.Holo.RatingBar.Indicator"> <item name="android:progressDrawable">@drawable/ratingbar</item> <item name="android:maxHeight">32dp</item> </style> <style name="CustomRatingBar2" parent="@android:style/Widget.Holo.RatingBar.Indicator"> <item name="android:progressDrawable">@drawable/ratingbar_yellow</item> <item name="android:maxHeight">32dp</item> </style> <!--> !--> <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">true</item> <item name="android:windowFullscreen">true</item> </style> <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" /> <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" /> <style name="CustomRatingBarsmall" parent="@android:style/Widget.Holo.RatingBar.Small"> <item name="android:progressDrawable">@drawable/ratingbarsmall</item> </style> <style name="ToolbarTheme" parent="@style/ThemeOverlay.AppCompat.Dark.ActionBar"> <!-- android:textColorPrimary is the color of the title text in the Toolbar --> <item name="android:textColorPrimary">@android:color/holo_blue_light</item> <!-- actionMenuTextColor is the color of the text of action (menu) items --> <item name="actionMenuTextColor">@android:color/holo_green_light</item> <!-- Tints the input fields like checkboxes and text fields --> <!-- Enable these below if you want clicking icons to trigger a ripple effect --> <!-- <item name="selectableItemBackground">?android:selectableItemBackground</item> <item name="selectableItemBackgroundBorderless">?android:selectableItemBackground</item> --> </style> <!-- This configures the styles for the title within the Toolbar --> <style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title"> <item name="android:textSize">21sp</item> <item name="android:textStyle">italic</item> </style> </resources> // -------- Logecat error: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.otherHand.mal3abak/com.otherHand.teamsnews}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2695) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2769) at android.app.ActivityThread.access$900(ActivityThread.java:177) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1430) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5910) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference at com.otherHand.teamsnews.SetupToolBar(teamsnews.java:33) at com.otherHand.teamsnews.onCreate(teamsnews.java:27) at android.app.Activity.performCreate(Activity.java:6178) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)

Toast