首页 > 解决方案 > 在 ViewSwitcher 中使用 EditText 并不能使其在第一次触摸时可编辑

问题描述

我用来在和ViewSwitcher之间切换。当我第一次单击 TextView 时,它会更改为 EditText,然后我必须再次单击 EditText 以便键盘出现并且我能够编辑文本。EditTextTextView

我想要的行为是能够从第一次单击 TextView 中编辑文本。我不希望用户每次想要编辑文本时都单击两次。

有什么办法吗?

这是一个代码示例,以使其更清晰:

XML 代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".Trial">

    <ViewSwitcher xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/user_name_switcher"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp">

        <TextView
            android:id="@+id/username_tv"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="12dp"
            android:paddingTop="8dp"
            android:text="User Name"
            android:textColor="@android:color/black"
            android:textSize="18sp" />

        <EditText
            android:id="@+id/username_et"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:hint="User Name"
            android:inputType="textPersonName" />
    </ViewSwitcher>

    <Button
        android:id="@+id/save_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="24dp"
        android:layout_marginTop="24dp"
        android:padding="12dp"
        android:text="save" />
</LinearLayout>

Java 代码

package com.example.test

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewSwitcher;

public class Trial extends AppCompatActivity {

    TextView userNameTV;
    EditText userNameET;
    Button saveBtn;

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

        userNameTV = findViewById(R.id.username_tv);
        userNameET = findViewById(R.id.username_et);
        saveBtn = findViewById(R.id.save_btn);

        saveBtn.setEnabled(false);

        userNameTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                textViewClicked();
                userNameET.setText(userNameTV.getText().toString());
            }
        });
        saveBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
                switcher.showNext();
                saveBtn.setEnabled(false);
            }
        });

    }

    public void textViewClicked() {
        ViewSwitcher switcher = findViewById(R.id.user_name_switcher);
        switcher.showNext();
        saveBtn.setEnabled(true);

    }
}

标签: androidcontenteditableviewswitcher

解决方案


推荐阅读