【问题标题】:How to open a new activity on click of textview in android?如何在android中单击textview时打开一个新活动?
【发布时间】:2018-03-12 13:57:09
【问题描述】:

我是 android 编程世界的新手,我设计了一个布局,其中包含用户名、密码 AS 编辑文本控件和“忘记密码”作为 textview 控件,单击/点击“忘记密码”时,我想打开一个新活动.

这是我正在尝试的,期待您的帮助。 我的应用程序的xml和java代码如下。

<?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"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_blue_light">

        <TextView
            android:id="@+id/txtVwAccountLogin"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:text="Account Login"
            android:textColor="@android:color/background_light"
            android:textSize="24sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.057" />

        <EditText
            android:id="@+id/eTxtUserName"
            android:layout_width="288dp"
            android:layout_height="48dp"
            android:background="@android:color/background_light"
            android:ems="10"
            android:hint=" Username"
            android:inputType="textPersonName"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.593"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.406" />

        <EditText
            android:id="@+id/eTxtPassword"
            android:layout_width="290dp"
            android:layout_height="45dp"
            android:background="@android:color/background_light"
            android:ems="10"
            android:hint=" Password"
            android:inputType="textPassword"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.606"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.535" />

        <TextView
            android:id="@+id/txtVwForgetPassword"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Forget Password ?"
            android:textColor="@android:color/background_light"
            android:textSize="14sp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.854"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.632" />

        <Button
            android:id="@+id/btnLogin"
            android:layout_width="128dp"
            android:layout_height="wrap_content"
            android:text="Login"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.779" />

        <Button
            android:id="@+id/btnRegister"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginBottom="8dp"
            android:layout_marginEnd="8dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="8dp"
            android:background="@android:color/holo_blue_light"
            android:text="Register"
            android:textColor="@android:color/background_light"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.15"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.662" />
    </android.support.constraint.ConstraintLayout>

Login.java

    public class Login extends AppCompatActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.login);

            final TextView tvForgotPwd = (TextView) findViewById(R.id.txtVwForgetPassword);

            tvForgotPwd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(Login.this, "you clicked me", Toast.LENGTH_SHORT).show();
                    Intent myIntent = new Intent(view.getContext(),PatientSearch.class);
                    startActivityForResult(myIntent, 0);
                }
            });
            final Button btnRegister = (Button) findViewById(R.id.btnRegister);

            btnRegister.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    Intent myIntent = new Intent(view.getContext(),MainActivity.class);
                    startActivityForResult(myIntent,0);
                }
            });

【问题讨论】:

标签: java android android-layout android-studio textview


【解决方案1】:

尽管这个问题已经被问了很多次,但我还是决定回答一下。这将起作用:

tvForgotPwd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent myIntent = new Intent(Login.this, PatientSearch.class);
        startActivity(myIntent);
    }
});

【讨论】:

  • 简单而好的答案。
【解决方案2】:

在 onCreate() 方法中,在 TextView 上设置 OnClickListener 回调。在回调中,使用 Intent 启动所需的活动。

下面是代码示例:

yourTextView= (TextView) findViewById(R.id.txtVwForgetPassword);

yourTextView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
            Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
            intent.setData(Uri.parse("https://youraddress.com"));    
            startActivity(intent);
    }
});

【讨论】:

  • onClick 属性在TextView 中的位置>
  • 确实;我删除了它,因为 onClick 侦听器是以编程方式设置的。不需要反对票!
【解决方案3】:

可能需要尝试一种方法

startActivity (new Intent (Activity.this, Open Activity.class));

【讨论】:

    猜你喜欢
    • 2013-04-10
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多