小编Gob*_*ber的帖子

使用Android数据绑定创建双向绑定

我已经实现了新的Android数据绑定,并在实现后意识到它不支持双向绑定.我试图手动解决这个问题,但我很难找到一个在绑定到EditText时使用的好解决方案.在我的布局中,我有这样的观点:

<EditText
android:id="@+id/firstname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords|textNoSuggestions"
android:text="@{statement.firstName}"/>
Run Code Online (Sandbox Code Playgroud)

另一种观点也显示了结果:

<TextView
style="@style/Text.Large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{statement.firstName}"/>
Run Code Online (Sandbox Code Playgroud)

在我的片段中,我创建了这样的绑定:

FragmentStatementPersonaliaBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_statement_personalia, container, false);
binding.setStatement(mCurrentStatement);
Run Code Online (Sandbox Code Playgroud)

这样可以将firstName的当前值放在EditText中.问题是如何在文本更改时更新模型.我尝试在editText上放置一个OnTextChanged-listener并更新模型.这创建了一个循环杀死我的应用程序(模型更新更新GUI,调用textChanged时间无穷大).接下来我尝试仅在发生真实变化时通知:

@Bindable
public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
        boolean changed = !TextUtils.equals(this.firstName, firstName);
        this.firstName = firstName;
        if(changed) {
            notifyPropertyChanged(BR.firstName);
        }
    }
Run Code Online (Sandbox Code Playgroud)

这样做效果更好,但每次我写一封信时,GUI都会更新,因为编辑光标会移到前面.

欢迎大家提出意见

android android-edittext android-databinding

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

删除错误消息时,不会删除TextInputLayout的错误视图

我有一个带有一些输入字段的垂直线性布局.使用TextInputLayout我得到一个带有标签和内置错误消息的漂亮流程.我的问题是当我添加和删除错误消息时.

如果我添加一条错误消息,它将位于编辑文本下方,一切看起来都不错.

如果我使用setError(null)删除错误消息,则消息将被删除,但空间仍然存在.这显然是按照googles设计的(参见https://code.google.com/p/android/issues/detail?id=176005).我非常希望删除这个空间,因为它会让UI看起来非常错误.

如果我执行.setErrorEnabled(false),视图将被删除,一切看起来都正常.但是,如果用户更改数据并执行另一个setError,则不会显示错误消息(仅编辑文本行为红色).

android android-design-library android-textinputlayout

10
推荐指数
2
解决办法
8988
查看次数

使用TextInputLayouts passwordToggleEnabled的可见密码

我正在使用TextInputLayout和支持库中的新函数:passwordToggleEnabled.这给了一个漂亮的"眼睛" - 图标,让用户可以打开和关闭密码可见性.

我的问题是,是否有办法使用此功能,但开始密码可见?

我的xml:

<android.support.design.widget.TextInputLayout
                    android:id="@+id/password"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:passwordToggleEnabled="true">

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

切换看起来类似于: 在此输入图像描述

我还没有找到一种方法在xml中执行此操作,而不是在呈现视图后手动切换可见性的方法.如果我将EditText的输入类型设置为textVisiblePassword,则不会显示切换.如果我在代码中使用例如mPasswordEditText.setTransformationMethod(null); 显示密码但切换消失,用户无法再次隐藏密码.我知道我可以手动完成所有操作,但只是想知道我是否可以使用新的魔术切换工作

android android-textinputlayout

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

如何在PreferenceActivity中将SharedPreferences设置为默认值?

我已经为PreferenceScreen创建了一个基于xml的PreferenceActivity.在xml中,您可以为不同的首选项指定默认值.但是在打开和关闭屏幕之前,这些不会存储在我的SharedPreferences中.

问题是我想立即使用存储在此屏幕中的首选项(如服务器地址),如果用户想要更改默认值,则只需要打开它.

有没有办法存储偏好屏幕xml中的所有首选项,而不强制用户打开和关闭首选项活动?

我知道你可以在从SharedPreferences中检索Preference时提供默认值,但是必须在xml和code中维护默认值是愚蠢的.

android preferences preferenceactivity sharedpreferences

7
推荐指数
1
解决办法
3203
查看次数

如何转换Jackson和Gson之间的日期?

在我们的Spring配置的REST服务器中,我们使用Jackson将对象转换为Json.该对象包含几个java.util.Date对象.

当我们尝试使用Gson的fromJson方法在Android设备上反序列化时,我们得到一个"java.text.ParseException:Unparseable date".我们尝试将日期序列化为自1970年以来相应于毫秒的时间戳,但得到相同的异常.

可以将Gson配置为将时间戳格式的日期(例如1291158000000)解析为java.util.Date对象吗?

java json timestamp jackson gson

5
推荐指数
1
解决办法
5911
查看次数

使用新的 prefixText 时如何更改 TextInputLayout 的提示填充?

我尝试TextInputLayout使用 new实现prefixText,使用com.google.android.material:material:1.2.0-alpha02. 这是一个非常酷的功能,但是当我添加前缀文本时,提示标签会浮动并在前缀之后对齐。这看起来不太好,特别是如果您在同一页面上有更多没有前缀的输入字段,浮动标签不会对齐。

截屏

我的布局代码的相关部分:

 <LinearLayout
       android:id="@+id/login_input_fields"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_username_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/username_hint"
        app:prefixText="Prefix">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/login_username_edit_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="number"
            android:singleLine="true" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_password_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/default_margin"
        android:imeOptions="actionDone"
        app:endIconMode="password_toggle">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/password_hint"
            android:inputType="textPassword"
            android:singleLine="true"/>

    </com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

android android-textinputlayout material-components material-components-android

4
推荐指数
1
解决办法
1749
查看次数

使用负页边距处理ViewPager中的点击事件

在此输入图像描述

我有一个ViewPager显示用户可以在其间滑动的"卡片".这些卡是在自己的片段中创建的(因此我使用FragmentStatePagerAdapter为ViewPager创建内容).

我想显示下一张卡片和可能是之前卡片的边缘,因此我在本网站上的建议中将ViewPager的pageMargin设置为负数.这很好用,看起来就像我想要的那样.

我的问题是我需要回复卡片上的点击事件.但是使用负的pageMargin会导致页面重叠,有时会导致错误的页面接收到click事件.如何确保正确的卡收到点击事件?基本上我想要的是只有当前/选定的页面才会收到点击事件.

从我的包含viewPager的主片段:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_cards_overview, null);

    mCardsViewPager = (ViewPager) root.findViewById(R.id.cards_viewpager);

    mPagerAdapter = new MyPagerAdapter(getActivity(), getFragmentManager(), mCardsList);
    mCardsViewPager.setAdapter(mPagerAdapter);
    mCardsViewPager.setOnPageChangeListener(mPagerAdapter);

    // Necessary or the pager will only have one extra page to show
    // make this at least however many pages you can see
    mCardsViewPager.setOffscreenPageLimit(3);

    // Set margin for pages as a negative number, so a part of next and 
    // previous pages will be showed (convert desired …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-viewpager

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

无法在TextInputLayout中的EditText中使用drawable

我最近将Android设计库从24.2.1升级到25.0.0.在此之后,EditText中的"drawableX"功能不起作用.

编辑01.11:我了解到如果你使用android:drawableStart而不是android:drawableLeft,在xml中设置drawable是有效的.但是以编程方式设置drawable不起作用.我使用它来制作一个"清除"按钮来清空EditText.但是这个功能现在已经破了.如果这是故意的谷歌或一个错误,我将不胜感激任何解决方法或知识!

我的可编辑文本的代码之前有效,但现在没有:

public class ClearableErrorTextInputEditText extends ErrorTextInputEditText implements View.OnFocusChangeListener, View.OnTouchListener, TextWatcher {

private Drawable resIcon;
private OnFocusChangeListener childFocusListener;

public ClearableErrorTextInputEditText(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    setup();
}

public ClearableErrorTextInputEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    setup();
}

public ClearableErrorTextInputEditText(Context context) {
    super(context);
    setup();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_UP) {
        if (getCompoundDrawables()[2] != null) {
            final boolean tappedClose = event.getX() > (getWidth() - getPaddingRight() - resIcon.getIntrinsicWidth());
            if (tappedClose) …
Run Code Online (Sandbox Code Playgroud)

android android-edittext android-design-library android-textinputlayout

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