1. 程式人生 > >Android 輸入框的輸入提示效果(AutoCompleteTextView)

Android 輸入框的輸入提示效果(AutoCompleteTextView)

在一些體驗較好的APP中,輸入框輸入時會有相應的提示,讓人能夠很快的通過點選提示進入下一步

這裡,我就通過自己構思,實現了一個通過 SharedPreferences 儲存的輸入提示 demo

實現:

1、實現一個 SharedPreferences 以及其 get、set方法

2、佈局一個  AutoCompleteTextView

3、通過java程式碼實現適配佈局、輸入提示的儲存

4、實際運用


效果:

AutoCompleteTextView 定義的輸入框和普通的 EditText 差不多


但是可以通過其他設定讓它有輸入提示功能


實現詳情:

1、實現一個 SharedPreferences 以及其 get、set方法

SharedPreferences 是一個輕量級的儲存類,這裡我拿它來做實驗,我設定最多可以儲存三個提示

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by Administrator on 2018/3/23 0023.
 * 定義 SharedPreferences 以及相應的 get、set 方法
 */
public class MySharedPreferences {
    //建立一個SharedPreferences    類似於建立一個數據庫,庫名為 data
public static SharedPreferences share(Context context){ SharedPreferences sharedPreferences = context.getSharedPreferences("date", Context.MODE_PRIVATE); return sharedPreferences; } //賬號1 public static String getPhone1(Context context){ return share(context).getString("phone1"
,null); } //這裡使用的是 apply() 方法儲存,將不會有返回值 public static void setPhone1(String phone1, Context context){ SharedPreferences.Editor e = share(context).edit(); e.putString("phone1",phone1); e.apply(); } //賬號2 public static String getPhone2(Context context){ return share(context).getString("phone2",null); } //這裡使用的是 apply() 方法儲存,將不會有返回值 public static void setPhone2(String phone2, Context context){ SharedPreferences.Editor e = share(context).edit(); e.putString("phone2",phone2); e.apply(); } //賬號3 public static String getPhone3(Context context){ return share(context).getString("phone3",null); } //這裡使用的是 apply() 方法儲存,將不會有返回值 public static void setPhone3(String phone3, Context context){ SharedPreferences.Editor e = share(context).edit(); e.putString("phone3",phone3); e.apply(); } }
SharedPreferences 可以靈活運用,還可以儲存一些其他的常用配置資訊、狀態資訊等

2、佈局一個  AutoCompleteTextView

<AutoCompleteTextView
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="賬號(一般是手機號)"/>

<EditText
android:id="@+id/pswd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="密碼"/>

<Button
android:id="@+id/sign_in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登入"/>

佈局很簡單,看上面的效果圖就知道了,只不過第一個輸入框是 AutoCompleteTextView

第二個是普通的 EditView 加了密碼效果

3、通過java程式碼實現適配佈局、輸入提示的儲存

這裡是重點,主要是為 AutoCompleteTextView 加入提示資料還有更新登入提示資料

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

    private AutoCompleteTextView phone;
    private EditText pswd;
    private Button sign_in;
    private String[] array;
    @Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        phone = (AutoCompleteTextView) findViewById(R.id.phone);

        array = new String[3];
        array[0] = MySharedPreferences.getPhone1(this)+"";
        array[1] = MySharedPreferences.getPhone2(this)+"";
        array[2] = MySharedPreferences.getPhone3(this)+"";

        //建立 AutoCompleteTextView 介面卡 (輸入提示)
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
                this,
                android.R.layout.simple_dropdown_item_1line,
                array);
        //初始化autoCompleteTextView
phone.setAdapter(adapter);
        //設定輸入多少字元後提示,預設值為2,在此設為1
phone.setThreshold(1);

        pswd = (EditText) findViewById(R.id.pswd);
        sign_in = (Button) findViewById(R.id.sign_in);


        sign_in.setOnClickListener(new View.OnClickListener() {
            @Override
public void onClick(View view) {
                //登入判斷。。。
                //這裡假設登入成功了 if 裡面的是判斷是否登入成功(這裡隨便寫的)
if((phone.getText().length()+pswd.getText().length()) >= 10){
                    //判斷登入的賬號有沒有儲存
phones(MainActivity.this, array, phone.getText().toString());
                }
                startActivity(new Intent(MainActivity.this, Main2Activity.class));
            }
        });

    }
    //判斷登入的賬號有沒有儲存,沒有就儲存起來,替換一個最久沒登入的
public static void phones(Context context, String[]array, String phone){
        //當 phone 是新登入的賬號的時候
boolean trfa = (!phone.equals(array[0]) && !phone.equals(array[1]) && !phone.equals(array[2]));
        if(trfa){
            //迴圈一下,將最後一個替換成新的
MySharedPreferences.setPhone3(array[1], context);
            MySharedPreferences.setPhone2(array[0], context);
            MySharedPreferences.setPhone1(phone, context);
        }
    }
}

具體都有註釋,可以仔細看看

4、實際運用

這裡我模仿登入成功,就跳轉到了另一個Activity去了,如果要檢視效果,就又跳轉回來實踐

這裡彈出來的提示資料都是在 SharedPreferences 裡的,它只能存少量資料

如果有需要可以將資料存入 SQLite 中去

原始碼:https://github.com/iscopy/AutoCompleteTextView

相關推薦

Android 輸入輸入提示效果AutoCompleteTextView

在一些體驗較好的APP中,輸入框輸入時會有相應的提示,讓人能夠很快的通過點選提示進入下一步這裡,我就通過自己構思,實現了一個通過 SharedPreferences 儲存的輸入提示 demo實現:1、實現一個 SharedPreferences 以及其 get、set方法2、

移動端輸入填坑系列

輸入在移動端是一個很常用的功能,那麼輸入框必然是一個很重要的部分。然而,移動端輸入框總會遇到各種各樣的問題,無論是樣式還是ios和android兩端體驗不一致都是很讓我們頭疼的問題,那麼如何使移動web的輸入框體驗更貼近原生也成了一個需要我們多多思考和研究的問題。 一、文字輸入

Android 實現輪播圖效果 底部圓點狀態改變

自動輪播和手動輪播之後應該實現圓點的切換 自定義改變圓點狀態的監聽器 新建介面public interface DotChangeListener,新增方法void dotChangeListener(int index);並在ImageBannerFramLayout實

純css3實現美化複選和手風琴效果詳細

關鍵技術點和原理: 原理就是把 checkbox或 radio 給隱藏掉   ,然後給選框 繫結一個label標籤。 然後用label標籤作為容器,在裡面放一個:before或一個after 用before模擬選框的框,用after來模擬選框的填充 通過 .magic-ch

android AutoCompleteTextView 實現輸入提示,類似百度支援輸入拼音提示中文gray

 android 的 AutoCompleteTextView 控制元件實現了輸入框的輸入提示功能,這個功能更加使用於國外的手機使用者來使用。而很多時候國人更多的是要象百度那樣我輸入的是拼音也能將中文提示出來,如:輸入xinlang  就能提示:新浪、新浪微博等。又或者

Android輸入自動提示進階--自定義佈局

發現Android的有兩種方法AutoCompleteTextView和MultiAutoCompleteTextView提示出來的提示框只是純文字而且是單條資料,要是想實現加一個圖片或者是每一條資料展示兩個資料呢,這就需要重寫介面卡設定佈局了 重寫介面卡: packag

Android輸入自動提示

Android用的有兩種方法AutoCompleteTextView和MultiAutoCompleteTextView,第二種可以連續提示輸入,如下圖   AutoCompleteTextView常用屬性  

Android元件系列1:自動完成輸入內容的元件AutoCompleteTextView

本文為原創,如需轉載,請註明作者和出處,謝謝!     AutoCompleteTextView 和 EditText 元件類似,都可以輸入文字。但 AutoCompleteText

bootstrap仿百度輸入智慧提示提示資訊存在於資料庫中

示例圖片: 百度示例 所實現示例 jsp: <form class="form-horizontal lui-tj-bd" id="dbc_billrecharge_add_form" method="post"><div clas

AutoCompleteTextView輸入自動提示功能

public class SuggestPoiAdapter extends BaseAdapter implements Filterable { private static final int ITEM_BAIDU = 0; private static final int CLOSE_

Javascript_06_表單驗證離開單項,輸入提示資訊

Javascript_06_ 表單驗證(離開單項,輸入框後提示資訊) 說明:對於必須輸入的入力框,游標離開(使用 onblur方法)時進行檢查。假如有錯,紅色的提示資訊直接在該畫面的這個輸入框的後面顯示出來,並把游標重新定向到這個輸入框。 待解決問題: ①    記憶體

NGUI的輸入的校驗input filed script

component inf 添加 tco 輸入框 body .com wake wak 一,我們制作一個輸入框,右鍵添加Sprite ,給Sprite添加一個child的label,然後給Sprite添加一個box collider,接著添加input filed scri

視訊網站開發:Ajax非同步實現搜尋輸入提示功能

        在我們實際專案的開發中,有時候一個小小的功能,可能都需要考慮很長的時間,因為必須把這個小功儘可能的做好做到極致。我的視訊網站從2018年年初做到現在,已經歷經了多半年的時光。由開始的功能單一,只能實現海量視訊的展示及免廣告播放功能;到現在的花樣

vue-cli中使用百度地圖根據輸入輸入的內容,獲取詳細地址

效果圖如下: 1.申請百度地圖金鑰(ak)-- http://lbsyun.baidu.com/index.php?title=jspopular (1)百度地圖開放平臺–開發文件–web開發–JavaScript API–服務介紹–產品簡介–申請金鑰(ak) (2)

Android搜尋自動提示文字——單一提示

AutoCompleteTextView(單一提示) package com.example.android_06; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import androi

百度地圖輸入關鍵字提示被模態擋住

.tangram-suggestion { z-index:9999; }新增到css裡就行了。 本來根據查到的資料。覺得是z-index的問題,但是調整各處都沒有效果。 後來看到了autocomplete。瞭解了一下之後,搜到了這個 https://yq.aliyun

c#TextBox輸入自動提示、自動完成、自動補全功能

功能概覽 相關屬性 TextBox.AutoCompleteCustomSource 屬性 獲取或設定當 TextBox.AutoCompleteSource 屬性設定為 [CustomSource] 時要使用的自定義 T:Sy

對頁面不能為空的檢驗,在輸入裡面提示不能為空的封裝

                <form action="">                     <div class="group">                         <label for="">留言內容:<

JAVA文字根據輸入內容自動模糊查詢動態

要做到文字框能根據自己輸入的值去查詢資料庫的東西需要一些準備,思路就是要當按下文字框的時候就會彈出一個表格,表格就是根據你輸入的值去查詢資料庫資料(對文字框增加刪除操作都可以動態查詢),然後就是要當查詢到了的資料需要雙擊表格需要的內容行把表格介面設定不可見,大致

文字下拉提示效果模擬百度效果提示

實現思想:當用戶在文字框中輸入時,可以利用ajax方式將文字框內內容傳給後臺的實現類,在實現類中經過處理後將產生的結果獲取過來在前臺呈現。具體操作可通過ajax將文字框的值通過ajax實現框架傳遞給系統後端獲得提示結果集,然後在文字框下側一下拉框的效果顯示出來供使用者選擇。