android - 在 ViewSwitcher 中使用 EditText 并不能使其在第一次触摸时可编辑
问题描述
我用来在和ViewSwitcher
之间切换。当我第一次单击 TextView 时,它会更改为 EditText,然后我必须再次单击 EditText 以便键盘出现并且我能够编辑文本。EditText
TextView
我想要的行为是能够从第一次单击 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);
}
}
解决方案
推荐阅读
- java - 我如何知道一个班级是否有很多责任以及如何消除这个问题
- javascript - AJAX 控制台结果 NaN 404 未找到
- python - 如何让 `pip freeze` 使用我的 Flask 模块的分支?
- sql-server - TSQL 选择符合条件的记录或第一个
- reactjs - 移动离线同步
- mysql - MySql:如何显示每个用户创建的所有任务?
- vue.js - Vue.js 如何将滚动位置设置回顶部?
- pandas - isinstance(df, pd._libs.tslib.Timestamp) 它有什么作用?
- c++ - 为什么 range::split_view 不是双向范围?
- javascript - 在鼠标 Y + 适应上更改 2 种背景颜色
到那个新颜色