Android开发经验谈Android开发安卓 解决方案

Android页面实现可编辑和不可编辑切换

2018-06-14  本文已影响93人  野岗狼沟兜
2.png
<?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>
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不能输入问题

将其中方法改动:

 private void setViewsEnable(boolean able) {
        for (View view : viewList) {
            view.setEnabled(able);
            view.setFocusable(able);
            view.setFocusableInTouchMode(able);
        }
    }
 private void setViewsEnable(boolean able) {
        for (View view : viewList) {
            view.setEnabled(able);
            view.setFocusable(able);
            if (view instanceof EditText) {
                view.setFocusableInTouchMode(able);
            }
        }
    }

更优雅的方式

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);
            }
        }
    }
}

上一篇 下一篇

猜你喜欢

热点阅读