国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android頁面中可編輯與不可編輯切換的實現

2019-10-21 21:48:25
字體:
來源:轉載
供稿:網友

前言

相信大家在開發中經常遇到這樣的需求,我們在某一頁面,點擊某可按鈕后,需要把顯示的頁面變為可編輯的頁面,以便修正數據,這樣的頁面該怎么實現呢?

先看截圖

Android,EditText,可編輯

Android,EditText,可編輯

<?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:

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不能輸入問題

將其中方法改動:

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

做出如上改動后,輸入問題倒是解決了,可是控件必須點擊兩次才響應,那么對比之前可以推測,屬性:
setFocusableInTouchMode導致了該問題,既然添加了該屬性后EditText正常,其他控件不正常,那么區別對待之,另做如下修改:

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

如此,我們的目標達到了,只是,正常情況下,我們這個頁面可能有十幾個、甚至幾十個控件需要操作,那么我們一個個找到之再添加到viewList中,丑不丑陋不好說,反正是搞得眼花繚亂就是,作為一個有抱負的碼農果斷不能忍!

更優雅的方式

既然問題是出在控件太多,一個個添加要操作控件太麻煩,那么可不可以遍歷布局尋找控件呢,可以的,將活動代碼做如下修改:

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的布局中,然后遍歷該布局容器即可,這種做法對那些總是發揮作用的控件集中在一起的話(比如都在頁面下半部分),還是比較方便的。

而文中做法勝在靈活,可以對任意控件做特殊操作。這部分就到這兒吧,希望能對你有用。

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對VEVB武林網的支持。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 隆昌县| 潮安县| 西城区| 娄烦县| 昔阳县| 封丘县| 溧阳市| 双辽市| 阿拉尔市| 南汇区| 白玉县| 双江| 青阳县| 准格尔旗| 肇东市| 石渠县| 双城市| 五寨县| 富顺县| 东山县| 广昌县| 平凉市| 织金县| 华安县| 牡丹江市| 扬州市| 钟山县| 湾仔区| 寿阳县| 土默特右旗| 石景山区| 顺平县| 长岭县| 黄骅市| 和田县| 建阳市| 濮阳市| 于都县| 化隆| 新宁县| 三都|