[英]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,需要用作按钮。此代码导致应用程序在运行时崩溃。
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.