标签: android-edittext

android EditText,键盘textWatcher问题

我正在开发一个Android应用程序,我有一个EditText,用户可以输入数字.我想使用不同的货币格式(比如##,##,###)来格式化数字,我想在运行中进行,即当用户输入每个数字时(而不是按下输入时).我google了一下,遇到了TextWatcher,我首先发现它很有希望,但结果却是一种绝对的痛苦.我正在HTC Desire手机上调试我的代码,它只有一个软键盘.

现在我想在用户按下数字(0到9),del(退格)键并输入密钥时收到回调.从我的测试中我发现了这些(至少在我的手机上)

1)当用户按del或输入键时,将调用editText onKeyListener.当用户按下回车键时,一次输入会调用onKey函数两次(我认为是ACTION_UP和ACTION_DOWN).当用户按下del时,onKey被调用一次(仅适用于ACTION_DOWN),我不知道为什么.用户按下任何数字(0到9)时都不会调用onKey,我也无法理解.

2)每当用户按任意数字(0到9)键时,都会调用TextWatchers 3个回调函数(beforeTextChanged,onTextChanged,afterTextChanged).所以我想通过一起使用TextWatcher和onKeyListener,我可以得到我需要的所有回调.

现在我的问题是这些......

1)首先在我的HTC软键盘上有一个键(带有向下箭头的键盘符号),当我点击它时键盘被重新签名而不给出任何回调.我仍然不能相信Android让用户编辑一个字段并辞职而不让程序处理(保存)编辑.现在我的editText显示一个值,我的对象有另一个值(我在输入时保存用户编辑,并通过使用对象中的值重置editText值来处理键盘按键,但我没有回答此键盘向下键) .

2)其次,我想在用户输入新数字后格式化数字.假设我在editText上已有123并且用户输入了按4,我希望我的editText显示1,234.我得到onTextChanged()和afterTextChanged()的完整数字,我可以格式化数字并将其放回到任何这些回调中的editText.我应该使用哪一个?哪种是最佳做法?

3)第三个是最关键的问题.当应用程序启动时,我将当前对象值放在editText中.假设我把123放在onResume()上,当用户输入一个数字(比如说4)时,我希望它是1234.但是在我的onTextChanged回调中我得到的是4123.当我再按一个键(比如5)我是得到45123.因此对于用户输入,editText光标指向文本的结尾.但是当手动设置值时,editText光标似乎不会更新.我相信我必须在textWatcher回调中做一些事情,但我不知道该怎么办.

我在下面发布我的代码.

public class AppHome extends AppBaseActivity {
    private EditText ed = null;
    private NumberFormat amountFormatter = null;
    private boolean  isUserInput = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_home_screen);

        ed = (EditText)findViewById(R.id.main_amount_textfield);
        amountFormatter = new DecimalFormat("##,##,###");


        ed.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if(event.getAction() == KeyEvent.ACTION_DOWN) 
                    return true;
                String strippedAmount = ed.getText().toString().replace(",", "");
                if(keyCode == KeyEvent.KEYCODE_DEL){
                    //delete pressed, strip …
Run Code Online (Sandbox Code Playgroud)

android formatter textwatcher android-edittext

14
推荐指数
1
解决办法
2万
查看次数

检测键盘搜索按钮

我更改android软键盘以显示搜索图标而不是Enter图标.我的问题是:如何检测用户点击该搜索按钮?

java android android-softkeyboard android-edittext

14
推荐指数
1
解决办法
1万
查看次数

在Android中的EditText左侧设置图像

我有一个联系表单,我想在我的编辑文本中添加一些图标以便更好地查看.我想创建类似下面的EditText.

在此输入图像描述

我有如下的EditText.我得到了类似的结果.我希望图像绝对在左侧,就像在上面的图像中一样.我知道内部EditText正在使用Nine Patch图像,我认为这就是为什么结果有些不同.谁能知道如何解决这个问题?

在此输入图像描述

<EditText
            android:id="@+id/name"
            android:layout_width="fill_parent"
            android:layout_height="51dip"
            android:hint="@string/name"
            android:inputType="text"
            android:drawableLeft="@drawable/name_icon_edittext" />
Run Code Online (Sandbox Code Playgroud)

android drawable android-edittext

14
推荐指数
2
解决办法
3万
查看次数

Android - 在EditText中更改*文本后执行某些操作*

我构建了一个Android应用程序.我有一个EditText.我想在用户更改文本后自动保存更改(或其他任何内容).现在我用

editText.addTextChangedListener(textWatcher);
Run Code Online (Sandbox Code Playgroud)

和

  TextWatcher textWatcher = new TextWatcher() {
    public void afterTextChanged(Editable s) {
     ...
    }
Run Code Online (Sandbox Code Playgroud)

但是它会在每次微小的更改后保存更改(添加\删除一个字母),我希望它只在用户完成后才能保存(关闭键盘,点击不同的编辑文本等).我怎样才能做到这一点?

android textwatcher android-edittext

14
推荐指数
2
解决办法
2万
查看次数

Android:在Number/Phone上设置inputType时,检查EditText是否为空

我在android中有一个EditText供用户输入他们的AGE.它设置为inputType = phone.我想知道是否有办法检查这个EditText是否为空.

我已经看过这个问题:检查EditText是否为空.但它没有解决inputType = phone的情况.

这些,我已经检查过并且不起作用:

(EditText) findViewByID(R.id.age)).getText().toString() == null
(EditText) findViewByID(R.id.age)).getText().toString() == ""
(EditText) findViewByID(R.id.age)).getText().toString().matches("")
(EditText) findViewByID(R.id.age)).getText().toString().equals("")
(EditText) findViewByID(R.id.age)).getText().toString().equals(null)
(EditText) findViewByID(R.id.age)).getText().toString().trim().length() == 0
(EditText) findViewByID(R.id.age)).getText().toString().trim().equals("")
and isEmpty do not check for blank space.
Run Code Online (Sandbox Code Playgroud)

谢谢您的帮助.

android gettext android-edittext

14
推荐指数
2
解决办法
7万
查看次数

即使setFocusable(true),PopupWindow中的EditText也不显示键盘

我似乎无法完成这项工作.我已经将popWindow设置为关注我在其他论坛上阅读的内容,但仍然没有运气.

XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:background="@drawable/popbg"
android:orientation="vertical" >

<Button
    android:id="@+id/cancel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:layout_marginRight="10dp"
    android:layout_marginTop="30dp"
    android:background="@drawable/zcancel" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="10dp"
    android:text="SSID"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<EditText
    android:id="@+id/editText3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:inputType="textPersonName" />
Run Code Online (Sandbox Code Playgroud)

java的

 case(R.id.settings):
 switch (event.getAction()) 
 {
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.drawable.cpanel2);
return true;
case MotionEvent.ACTION_UP:
v.setBackgroundResource(R.drawable.cpanel1);

LayoutInflater layoutInflater =  
    (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  

View popSwitchView = layoutInflater.inflate(R.layout.settings_xml, null);

final PopupWindow popWindow = new PopupWindow(popSwitchView);
popWindow.setWidth(LayoutParams.MATCH_PARENT);
popWindow.setHeight(LayoutParams.MATCH_PARENT);
popWindow.showAtLocation(popSwitchView, Gravity.CENTER, 0, 0);
popWindow.setOutsideTouchable(false);  
popWindow.setFocusable(true);
Drawable d = getResources().getDrawable(R.drawable.popbg); 
popWindow.setBackgroundDrawable(d);

Button …
Run Code Online (Sandbox Code Playgroud)

keyboard android popupwindow android-edittext

14
推荐指数
2
解决办法
1万
查看次数

TextInputLayout提示颜色

我正在使用新的Google设计库(com.android.support:design:22.2.0),我遇到了android.support.design.widget.TextInputLayout的问题.

如果我以编程方式设置其EditText,浮动提示颜色是灰色而不是强调颜色.如果用户填充字段(EditText)本身或者他更改了字段的预定义值,它就可以工作.

这是我的代码:

<android.support.design.widget.TextInputLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent">
        <EditText
            android:id="@+id/register_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/register_username"
            android:inputType="text"/>
</android.support.design.widget.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)

用户单击某个按钮后,它通过EditText的setText()方法填充字段,浮动提示变为灰色.是错误还是我错过了什么?

android android-edittext android-design-library

14
推荐指数
3
解决办法
2万
查看次数

自定义键盘:处理inputType更改

我遇到了一个我无法弄清楚的问题.我根据这个示例编写了一个简单的自定义IME键盘.

它基本上有两个自定义键盘,一个用于字母,一个用于数字.他们使用不同的布局.

但是,当我EditText为文本添加两个控件时,为数字添加一个控件时,键盘不会刷新到它所属的类型.我的意思是,如果我选择了EditText与inputType="text"第一,QWERTY键盘布局出现.但后来当我选择第二个EditText与inputType="number"QWERTY键盘再次出现.但是,它应该为连接到代码中的数字加载不同的布局.

换句话说,这是测试活动布局:

在此输入图像描述

现在,如果我选择"文本"字段,QWERTY键盘将显示如下:

在此输入图像描述

但是,如果我选择"数字"字段,QWERTY键盘仍会显示错误.

在此输入图像描述

预期的行为将是这个键盘出现.

在此输入图像描述

这是CustomIME的代码,我试图postInvalidate()在视图上使用,预加载所有布局onInitializeInterface()但没有任何效果.它永远不会正确切换到数字的布局

public class CustomIME extends InputMethodService
        implements KeyboardView.OnKeyboardActionListener {

    public static final String CUSTOM_IME = "CUSTOM_IME";
    private KeyboardView mKeyboardView;
    private Keyboard mKeyboardCurrent;
    private KeyboardType mKeyboardTypeCurrent = KeyboardType.QWERTY_LETTERS;
    private boolean mCAPs = false;


    enum KeyboardType {
        QWERTY_LETTERS,
        NUMBERS
    }

    @Override
    public View onCreateInputView() {
        loadCurrentKeyboard();
        mKeyboardView = (KeyboardView) getLayoutInflater().inflate(R.layout.custom_ime_keyboard, null);
        mKeyboardView.setBackgroundResource(R.drawable.btn_gradient);
        mKeyboardView.setOnKeyboardActionListener(this);

        if (mKeyboardCurrent != null) {
            mKeyboardView.setKeyboard(mKeyboardCurrent); …
Run Code Online (Sandbox Code Playgroud)

java android android-layout android-softkeyboard android-edittext

14
推荐指数
1
解决办法
1733
查看次数

自动调整EditText

Android最近添加了对基于视图大小和最小和最大文本大小调整TextViews文本大小的支持.
https://developer.android.com/guide/topics/ui/look-and-feel/autosizing-textview.html

不幸的是,它们不支持EditTexts,那么EditText还有其他选择吗?

android autosize android-edittext

14
推荐指数
1
解决办法
6183
查看次数

使用TextInputLayout时出现InflateException

我正在尝试使用Material Design中的TextInputEditText(https://github.com/material-components/material-components-android/blob/master/docs/components/TextInputLayout.md),我得到了运行时异常.

这是运行日志的一部分:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.grigori.materialtextedit, PID: 12036
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grigori.materialtextedit/com.example.grigori.materialtextedit.MainActivity}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
                  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
                  at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
                  at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
                  at android.os.Handler.dispatchMessage(Handler.java:106)
                  at android.os.Looper.loop(Looper.java:193)
                  at android.app.ActivityThread.main(ActivityThread.java:6669)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
               Caused by: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: Error inflating class com.google.android.material.textfield.TextInputLayout
               Caused by: android.view.InflateException: Binary …
Run Code Online (Sandbox Code Playgroud)

android android-edittext android-textinputlayout android-textinputedittext

14
推荐指数
10
解决办法
1万
查看次数