服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android页面中可编辑与不可编辑切换的实现

Android页面中可编辑与不可编辑切换的实现

2022-03-09 15:00野岗狼沟兜 Android

这篇文章主要给大家介绍了关于在Android页面中可编辑与不可编辑切换的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

相信大家在开发中经常遇到这样的需求,我们在某一页面,点击某可按钮后,需要把显示的页面变为可编辑的页面,以便修正数据,这样的页面该怎么实现呢?

先看截图

Android页面中可编辑与不可编辑切换的实现

Android页面中可编辑与不可编辑切换的实现

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:id="@+id/all_views"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context=".MainActivity">
 
 <Button
 android:id="@+id/edit"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="编辑"
 android:textSize="25sp" />
 
 <RadioGroup
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:orientation="horizontal">
 
 <RadioButton
 android:id="@+id/boy"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="男"
 android:textSize="25sp" />
 
 <RadioButton
 android:id="@+id/girl"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="女"
 android:textSize="25sp" />
 </RadioGroup>
 
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_weight="1">
 
 <EditText
 android:id="@+id/views"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@null"
 android:hint="代表一大堆控件"
 android:textSize="25sp" />
 </LinearLayout>
 
 <Button
 android:id="@+id/special"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="一个在编辑状态和不可编辑状态都要用的Button"
 android:textSize="25sp" />
</LinearLayout>

活动 MainActivity.java:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 Button edit, special;
 LinearLayout linearLayout;
 RadioButton boy, girl;
 EditText views;
 
 List<View> viewList = new ArrayList<>();
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 edit = (Button) findViewById(R.id.edit);
 
 special = (Button) findViewById(R.id.special);
 linearLayout = (LinearLayout) findViewById(R.id.all_views);
 edit.setOnClickListener(this);
 special.setOnClickListener(this);
 
 boy = (RadioButton) findViewById(R.id.boy);
 girl = (RadioButton) findViewById(R.id.girl);
 views = (EditText) findViewById(R.id.views);
 
 viewList.add(boy);
 viewList.add(girl);
 viewList.add(views);
 
 setViewsEnable(false);
 }
 
 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.edit) {
 if (edit.getText().toString().equals("编辑")) {
 edit.setText("完成");
 setViewsEnable(true);
 } else {
 edit.setText("编辑");
 setViewsEnable(false);
 }
 } else if (v.getId() == R.id.special) {
 Toast.makeText(this, "我总是有用的那个", Toast.LENGTH_SHORT).show();
 }
 }
 
 private void setViewsEnable(boolean able) {
 for (View view : viewList) {
 view.setEnabled(able);
 view.setFocusable(able);
 
 }
 }
}

这样基本的要求达到了,但是还有个问题:EditText不能输入了 !,就上述代码,id为views的EditText无论在那种状态都不能输入了。

点击两次才响应和EditText不能输入问题

将其中方法改动:

?
1
2
3
4
5
6
7
private void setViewsEnable(boolean able) {
for (View view : viewList) {
 view.setEnabled(able);
 view.setFocusable(able);
 view.setFocusableInTouchMode(able);
}
}

做出如上改动后,输入问题倒是解决了,可是控件必须点击两次才响应,那么对比之前可以推测,属性:
setFocusableInTouchMode导致了该问题,既然添加了该属性后EditText正常,其他控件不正常,那么区别对待之,另做如下修改:

?
1
2
3
4
5
6
7
8
9
private void setViewsEnable(boolean able) {
for (View view : viewList) {
 view.setEnabled(able);
 view.setFocusable(able);
 if (view instanceof EditText) {
 view.setFocusableInTouchMode(able);
 }
}
}

如此,我们的目标达到了,只是,正常情况下,我们这个页面可能有十几个、甚至几十个控件需要操作,那么我们一个个找到之再添加到viewList中,丑不丑陋不好说,反正是搞得眼花缭乱就是,作为一个有抱负的码农果断不能忍!

更优雅的方式

既然问题是出在控件太多,一个个添加要操作控件太麻烦,那么可不可以遍历布局寻找控件呢,可以的,将活动代码做如下修改:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.example.softdk.myapplication;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.Toast;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
 Button edit, special;
 LinearLayout allViews;
 RadioButton boy, girl;
 EditText views;
 
 List<View> viewList = new ArrayList<>();
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 edit = (Button) findViewById(R.id.edit);
 
 special = (Button) findViewById(R.id.special);
 allViews = (LinearLayout) findViewById(R.id.all_views);
 edit.setOnClickListener(this);
 special.setOnClickListener(this);
 
 traversalView(allViews);
 setViewsEnable(false);
 }
 
 @Override
 public void onClick(View v) {
 if (v.getId() == R.id.edit) {
  if (edit.getText().toString().equals("编辑")) {
  edit.setText("完成");
  setViewsEnable(true);
  } else {
  edit.setText("编辑");
  setViewsEnable(false);
  }
 } else if (v.getId() == R.id.special) {
  Toast.makeText(this, "我总是有用的那个", Toast.LENGTH_SHORT).show();
 }
 }
 
 private void traversalView(ViewGroup viewGroup) {
 int i = viewGroup.getChildCount();
 for (int j = 0; j < i; j++) {
  View view = viewGroup.getChildAt(j);
  if (view.getId() == R.id.edit)
  continue;//除去我们 编辑-完成 按钮,正常使用情况下一般是在标题栏上添加监听,不会有这个情况=
  else if (view.getId() == R.id.special)
  continue;//除去那些我们再 编辑-完成 状态都需要起作用的按钮
  viewList.add(view);//找所有布局和控件
  if (view instanceof ViewGroup) {
  /**
   * viewList.add(view);//只找布局
   *
   * 注意此处,如果该空间是布局容器,那么继续寻找布局内部的控件
   * 直到找到的控件不是布局容器
   * 如果我们想找的控件包括了布局容器(如LinearLayout之类的里面能放控件的东西)
   * 那么应该在该判读之前将找到的view添加到我们的集合
   * 如果仅仅是想找控件,那么在else之内添加(下面注释掉了)
   */
  traversalView((ViewGroup) view);
  } else {
  // viewList.add(view);//只找控件
  }
 }
 }
 
 private void setViewsEnable(boolean able) {
 for (View view : viewList) {
  view.setEnabled(able);
  view.setFocusable(able);
  if (view instanceof EditText) {
  view.setFocusableInTouchMode(able);
  }
 }
 }
}

不卖关子了,上面就是完整版,去掉注释,逻辑还是很简单清晰的,如果结合Butterknife等框架插件使用的话,能大大减少琐碎代码的编写。注意看下那两句continue其实一个意思,除去我们想让它一直发挥作用的控件,其实还有一种方法是:

将我们需要改变状态的控件放到一个类似于文中id为all_views的布局中,然后遍历该布局容器即可,这种做法对那些总是发挥作用的控件集中在一起的话(比如都在页面下半部分),还是比较方便的。

而文中做法胜在灵活,可以对任意控件做特殊操作。这部分就到这儿吧,希望能对你有用。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/0931ff152cb3

延伸 · 阅读

精彩推荐