gpt4 book ai didi

android - 我将 TextView 用作按钮,但它无法执行其操作。按下按钮后应用程序总是崩溃

转载 作者:行者123 更新时间:2023-11-29 22:58:53 29 4
gpt4 key购买 nike

我正在尝试将 TextView 用作 Button 但它无法执行其操作。按下此 TextView 按钮后,应用程序总是停止工作。

我在我的 XML 代码中添加了 android:clickable="true"android:onClick="perform_action" 部分。但是每当我按下按钮时,应用程序就会停止工作。

这是一个 Activity_main.xml 代码(它不包含我试图用作按钮的 TextView):

<?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/btn1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:visibility="visible">

<Button
android:id="@+id/ok"
android:layout_width="97dp"
android:layout_height="42dp"
android:layout_marginStart="157dp"
android:layout_marginLeft="157dp"
android:layout_marginTop="62dp"
android:layout_marginEnd="157dp"
android:layout_marginRight="157dp"
android:layout_marginBottom="371dp"
android:text="Нэвтрэх"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/etToken" />

<EditText
android:id="@+id/etToken"
android:layout_width="282dp"
android:layout_height="47dp"
android:layout_marginStart="64dp"
android:layout_marginLeft="64dp"
android:layout_marginTop="209dp"
android:layout_marginEnd="65dp"
android:layout_marginRight="65dp"
android:layout_marginBottom="25dp"
android:ems="10"
android:hint="Token-оо оруулаад нэвтэрнэ үү!"
android:inputType="textPassword"
app:layout_constraintBottom_toTopOf="@+id/ok"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

这是我的主要 Activity 。主要 Activity 将用户引导到另一个页面(或 fragment ),其中包含我试图用作按钮的 TextView(就像一个简单的登录部分)。:

    package com.example.demo;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Set;

public class MainActivity extends AppCompatActivity {
private EditText txt;
private String token = "123456";
private Button btn, btn1;
private String Anhaaramj_ogoh_ortoo;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView ortoohead = findViewById(R.id.ortoohead);
txt = findViewById(R.id.etToken);
btn = findViewById(R.id.ok);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

check(txt.getText().toString());
}
});
}
public void showAlert(View view){
AlertDialog.Builder alert1 = new AlertDialog.Builder(this);
alert1.setMessage("Уучлаарай таны оруулсан token буруу байна!")
.setPositiveButton("Дахин оролдох", new
DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.create();
alert1.show();
}
public void check(String Tkn){
if(Tkn.equals(token)){
Intent intent = new Intent(MainActivity.this, harnaa.class);
txt.setText(null);
startActivity(intent);
}
else{
showAlert(null);
txt.setText(null);
}
}

public void perform_action(View v)
{
TextView tv = findViewById(R.id.ortoohead);

//alter text of textview widget
tv.setText("This text view is clicked");

//assign the textview forecolor
tv.setTextColor(Color.GREEN);
}

activity_attention.xml 包含我的 TextView,这是最后一个 TextView:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/valuebody"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@id/valuehead"
android:text="Анхаарамж өгөх болсон шалтгаанаа бичнэ үү?" />

<TextView
android:id="@+id/valuehead"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/epbody"
android:text="Анхаарамжийн утга:"
android:textColor="#000000"
android:textSize="30dip" />

<TextView
android:id="@+id/epbody"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@id/ephead"
android:text="КП-КМ"
android:textSize="15dip" />

<TextView
android:id="@+id/ephead"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/spbody"
android:text="Дуусах цэг:"
android:textColor="#000000"
android:textSize="30dip" />

<TextView
android:id="@+id/spbody"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@id/sphead"
android:text="КМ-ПК"
android:textSize="15dip" />

<TextView
android:id="@+id/sphead"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/posbody"
android:text="Эхлэх Цэг:"
android:textColor="#000000"
android:textSize="30dip" />

<TextView
android:id="@+id/posbody"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@id/poshead"
android:text="Таны байршил"
android:textSize="15dip" />

<TextView
android:id="@+id/poshead"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/hoorondbody"
android:text="Ажил хийх байршил:"
android:textColor="#000000"
android:textSize="30dip" />

<TextView
android:id="@+id/hoorondbody"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@id/hoorondhead"
android:layout_marginTop="2dp"
android:text="Наран-Элгэн-Оорцог-Энгэр"
android:textSize="15dip" />

<TextView
android:id="@+id/hoorondhead"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/ehlehbody"
android:text="Хоорондын зам:"
android:textColor="#000000"
android:textSize="30dip" />
<TextView
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="@+id/ehlehbody"
android:layout_below="@id/ehlehhead"
android:text="2019/07/25 22:35"
android:textSize="15dip"/>

<TextView
android:id="@+id/ehlehhead"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/ortoobody"
android:text="Эхлэх огноо:"
android:textColor="#000000"
android:textSize="30dip" />

<TextView
android:id="@+id/ortoobody"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_below="@id/ortoohead"
android:text="Өртөөний нэр"
android:textSize="15dip"
android:clickable="true"/>

<TextView
android:id="@+id/ortoohead"
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="true"
android:onClick="perform_action"
android:text="Анхаарамж өгөх өртөөд:"
android:textColor="#000000"
android:textSize="30dip" />

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/valuebody"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="22dp"
android:layout_marginEnd="36dp"
android:layout_marginRight="36dp"
android:onClick="SetOrtoo"
android:text="Button" />


</RelativeLayout>

最后Attention.java it a Fragment:

    package com.example.demo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Attention extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_attention, container, false);
}

}

最佳答案

当您尝试从 fragment 中单击 TextView 时,您需要像这样实现 -

只需将您的 Attention fragment 代码替换为以下 fragment 即可。

package com.example.demo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class Attention extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_attention, container, false);
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
TextView ortooheadTextView = getActivity().findViewById(R.id.ortoohead);
ortooheadTextView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "TextView clicked.", Toast.LENGTH_LONG).show();
}
});
}

}

关于android - 我将 TextView 用作按钮,但它无法执行其操作。按下按钮后应用程序总是崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57156717/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com