简体   繁体   English

如何在Android Studio中使TextView充当按钮?

[英]How to make a TextView act as a button in Android Studio?

I want a TextView to respond to click and open a second Activity.The Main Activity contains two text fields, a button and another TextView, which needs to act as a button.This code results in crashing of the App whenever it is run. 我想让TextView响应单击并打开第二个Activity.Main Activity包含两个文本字段,一个按钮和另一个TextView,需要用作按钮。此代码导致应用程序在运行时崩溃。

应用程序的MainActivity

Here is the MainActivity.java file.- 这是MainActivity.java文件。

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView t1 = (TextView)findViewById(R.id.textView2);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void onSignUpClick(View view){
t1.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(".SignUpActivity");
                startActivity(intent);
            }
        }
);
}
}

Here is the AndroidManifest.xml file.- 这是AndroidManifest.xml文件。-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abinas.myapplication">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".SignUpActivity"
        android:parentActivityName=".MainActivity">
    </activity>
</application>

</manifest>

What's the error? 怎么了 Try changin your Intent for this 尝试改变您的意图

Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
startActivity(intent);

Change the Intent to something with this structure: 使用以下结构将Intent更改为某种内容:

new Intent( current class.this, target activity.class)

In the code it should look like: 在代码中,它应类似于:

  public void onSignUpClick(View view){
    t1.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MainActivity.this, SignUpActivity.class)
                    startActivity(intent);
                }
            }
    );

Assuming you are calling onSignUpClick from you XML it should be as simple as: 假设您要从XML调用onSignUpClick,它应该很简单:

public void onSignUpClick(View view){
   Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
   startActivity(intent);            
}

Alternatively you could just create the listener when activity is created: 另外,您可以仅在创建活动时创建侦听器:

public class MainActivity extends AppCompatActivity {

    TextView t1;

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

        t1 = (TextView)findViewById(R.id.textView2);

        t1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 Intent intent = new Intent(MainActivity.this, SignUpActivity.class);
                 startActivity(intent); 

            }
        }           
        );

    }

}

All the view widgets can override onclick method, but you are doing wrong you are using onclick method inside of another onclick method, and another thing is you should use findViewById inside onCreate method. 所有视图小部件都可以覆盖onclick方法,但是您在另一个onclick方法中使用onclick方法时做错了,另一件事是您应该在onCreate方法中使用findViewById。

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

TextView t1 = (TextView)findViewById(R.id.textView2);
t1.setOnClickListener(
      new View.OnClickListener() {
         @Override
         public void onClick(View v) {
             Intent intent = new Intent(context,SignUpActivity.class);
             startActivity(intent);
        }
    }
);
 }



}

Move this code inside your onCreate 将此代码移到onCreate中

TextView t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    t1 = (TextView)findViewById(R.id.textView2);
    t1.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(".SignUpActivity");
                startActivity(intent);
            }
        }
  );
}

You need to wrap you text view inside a linear layout and make the linear layout clickable. 您需要将文本视图包装在线性布局中,并使线性布局可单击。

<LinearLayout
    android:id="@+id/linear_forgot_password"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/spacing_small"
    android:orientation="vertical"
    android:clickable="true"
    android:focusable="true"
    app:layout_constraintHorizontal_bias="1.0"
    app:layout_constraintEnd_toEndOf="@+id/btn_enter"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/btn_enter">

    <androidx.appcompat.widget.AppCompatTextView
        android:id="@+id/tv_forgot_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorAccent"
        tools:text="Forgot password?"
         />
</LinearLayout>

An then get the onclick event in you activity or fragment. 然后在您的活动或片段中获取onclick事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使数字从 60 运行到 0 并在 android studio 中的 textview 上打印 - how to make number running from 60 to 0 and print on textview in android studio libgdx使android后退按钮的作用类似于主页按钮 - libgdx make android back button act like home button Android Studio,AppCompatButton的子类不像按钮 - Android studio, subclass of AppCompatButton does not act like a button Android Studio 按钮清除 TextView 文本 - Android Studio Button to clear TextView text 如何在 Android Studio 中制作“如果单击按钮,则执行此操作”? - How to make 'if button clicked, do this' in Android Studio? 在按下按钮之前使Android TextView不可见? - Make Android TextView invisible until button pressed? 如何在 Android Studio 中的按钮单击上创建一个新按钮? - How to make a new button on a button click in Android Studio? 如何列出JLabel的按钮组列表? - How to make a list of JLabel's act as a button group? 如何在android studio中点击按钮时从另一个活动更改textview文本? - How to change textview text from another activity on button click in android studio? Android Studio-每次单击按钮时如何在线性布局上生成新的textview - Android Studio - How can I generate a new textview on my linear layout every time i click a button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM