Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 項目實戰(二十七):資料填寫與編輯對話框設計總結

編輯:關於 Android 開發

Android 項目實戰(二十七):資料填寫與編輯對話框設計總結

前言:專案中一定會遇到資料填寫的需求,例如修改使用者名稱的文字編輯對話框、選擇生日的日期對話框等等。這裡做個整理,方便日後取用。

註:本文先說明實作過程,想學習的朋友可以細看;只想直接使用的朋友,也可以跳到文末複製完整程式碼。

一、文字編輯對話框

先看效果圖(仿今日頭條):

包含:一個標題 TextView、一個圓角白色背景的 EditText、一個可顯示剩餘輸入字數提示的 TextView,以及「確定」、「取消」兩個按鈕。

(1)編寫版面配置檔

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:background="@color/grey_5"
 android:id="@+id/popup_edit_info_ly" >
 <!--標題-->
 <TextView android:id="@+id/popup_edit_info_txt_title"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="title"
 android:gravity="center_horizontal"
 android:padding="@dimen/dp_6"
 android:textColor="@color/black" />
 <!--編輯框-->
 <EditText android:id="@+id/popup_edit_info_edit_content"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="@dimen/dp_6"
 android:layout_marginRight="@dimen/dp_6"
 android:background="@drawable/bg_popup_edit"
 android:maxLength="10"
 android:padding="@dimen/dp_6" >
 </EditText>
 <RelativeLayout android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:padding="@dimen/dp_6" >
 <!--提示文字-->
 <TextView android:id="@+id/popup_edit_info_txt_tip"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:textSize="@dimen/txt_10"
 android:text="剩余可輸入個數:"
 android:layout_centerVertical="true"
 android:layout_marginLeft="@dimen/dp_6" />
 <!--確定按鈕,這裡用 TextView,當然也可以用 Button、ImageButton-->
 <TextView android:id="@+id/popup_edit_info_btn_confirm"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/confirm"
 android:background="@drawable/bg_btn_blue"
 android:padding="@dimen/dp_4"
 android:textColor="@color/white"
 android:layout_alignParentRight="true" />
 <!--取消按鈕-->
 <TextView android:id="@+id/popup_edit_info_btn_cancel"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/cancel"
 android:background="@drawable/bg_btn_grey"
 android:padding="@dimen/dp_4"
 android:textColor="@color/white"
 android:layout_toLeftOf="@id/popup_edit_info_btn_confirm"
 android:layout_marginRight="@dimen/dp_10" />
 </RelativeLayout>
</LinearLayout>

dialog_edit_txt。其中的編輯框 EditText 用到圓角白色背景,需要另外寫一個 drawable 檔:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android">
 <!--popupwindow 編輯框的背景顏色,用於 popup_edit_info.xml 版面配置-->
 <solid android:color="@color/white" ></solid>
 <corners android:radius="@dimen/dp_6"></corners>
 <stroke android:width="0.5dp" android:color="@color/grey_1"></stroke>
</shape>

bg_popup_edit

(2)在 Activity 或 Fragment 中使用

① 把對話框封裝在一個方法裡,方便重複呼叫:

title 文字對話框的標題
isSingleLine EditText 是否限制單行顯示
maxSize EditText 中文字的最大長度
textview 要修改的 TextView 控制項,先將其文字顯示在 EditText 中,點擊確定後再將編輯結果寫回該控制項

private void showEditDialog(String title , boolean isSingleLine , final int maxSize, final TextView textview)

② 依版面配置產生 view 並初始化控制項:

View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_txt, null);
TextView popup_edit_info_txt_title;//標題
final TextView popup_edit_info_txt_tip; //編輯框剩餘個數提示
TextView popup_edit_info_btn_cancel; //取消按鈕
TextView popup_edit_info_btn_confirm; //確定按鈕
final EditText popup_edit_info_edit_content; //編輯框
popup_edit_info_txt_title = (TextView) view.findViewById(R.id.popup_edit_info_txt_title);
popup_edit_info_txt_tip = (TextView) view.findViewById(R.id.popup_edit_info_txt_tip);
popup_edit_info_btn_cancel = (TextView) view.findViewById(R.id.popup_edit_info_btn_cancel);
popup_edit_info_btn_confirm = (TextView) view.findViewById(R.id.popup_edit_info_btn_confirm);
popup_edit_info_edit_content = (EditText) view.findViewById(R.id.popup_edit_info_edit_content);

③ 進行控制項的屬性設定:

popup_edit_info_edit_content.setText(textview.getText().toString()); //將 textview 的文字顯示在 EditText 中
popup_edit_info_edit_content.setSingleLine(isSingleLine); //是否單行,像使用者名稱需單行,評價簡介則不需要
popup_edit_info_edit_content.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxSize)}); //限制最大長度
popup_edit_info_txt_tip.setText("剩余可輸入個數:"+(maxSize-textview.getText().toString().length())); //剩餘字數提示
final AlertDialog dialog = new AlertDialog.Builder(this) //建立對話框
 .setView(view)
 .create();
popup_edit_info_txt_title.setText(title); //設定標題
dialog.setCanceledOnTouchOutside(false); //點擊螢幕時對話框不消失

④ 為 EditText 加上文字變動監聽,即時更新提示文字:

popup_edit_info_edit_content.addTextChangedListener(new TextWatcher() {
 @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
 @Override public void afterTextChanged(Editable s) {
 int length = s.length();
 popup_edit_info_txt_tip.setText("剩余可輸入個數:"+(maxSize-length));
 }
});

⑤ 設定「確定」、「取消」的點擊事件:

popup_edit_info_btn_cancel.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) { dialog.dismiss(); }
});
popup_edit_info_btn_confirm.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 textview.setText(popup_edit_info_edit_content.getText().toString());
 dialog.dismiss();
 }
});

⑥ 顯示對話框:

dialog.show();

二、日期選擇對話框(敬請期待)

……(後續待續)


完整程式碼(文字編輯對話框)

版面配置檔 dialog_edit_txt、drawable 檔 bg_popup_edit 同上,Java 檔呼叫方式如下:

showEditDialog("請填寫姓名",true,10,edit_info_txt_name);
private void showEditDialog(String title , boolean isSingleLine , final int maxSize, final TextView textview) {
 View view = LayoutInflater.from(this).inflate(R.layout.dialog_edit_txt, null);
 TextView popup_edit_info_txt_title;
 final TextView popup_edit_info_txt_tip;
 TextView popup_edit_info_btn_cancel;
 TextView popup_edit_info_btn_confirm;
 final EditText popup_edit_info_edit_content;
 popup_edit_info_txt_title = (TextView) view.findViewById(R.id.popup_edit_info_txt_title);
 popup_edit_info_txt_tip = (TextView) view.findViewById(R.id.popup_edit_info_txt_tip);
 popup_edit_info_btn_cancel = (TextView) view.findViewById(R.id.popup_edit_info_btn_cancel);
 popup_edit_info_btn_confirm = (TextView) view.findViewById(R.id.popup_edit_info_btn_confirm);
 popup_edit_info_edit_content = (EditText) view.findViewById(R.id.popup_edit_info_edit_content);
 popup_edit_info_edit_content.setText(textview.getText().toString());
 popup_edit_info_edit_content.setSingleLine(isSingleLine);
 popup_edit_info_edit_content.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxSize)});
 popup_edit_info_txt_tip.setText("剩余可輸入個數:"+(maxSize-textview.getText().toString().length()));
 final AlertDialog dialog = new AlertDialog.Builder(this).setView(view).create();
 popup_edit_info_txt_title.setText(title);
 popup_edit_info_edit_content.addTextChangedListener(new TextWatcher() {
 @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
 @Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
 @Override public void afterTextChanged(Editable s) {
 int length = s.length();
 popup_edit_info_txt_tip.setText("剩余可輸入個數:"+(maxSize-length));
 }
 });
 popup_edit_info_btn_cancel.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) { dialog.dismiss(); }
 });
 popup_edit_info_btn_confirm.setOnClickListener(new View.OnClickListener() {
 @Override public void onClick(View v) {
 textview.setText(popup_edit_info_edit_content.getText().toString());
 dialog.dismiss();
 }
 });
 dialog.setCanceledOnTouchOutside(false);
 dialog.show();
}

效果圖:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved