小编asc*_*sco的帖子

按下向下箭头时隐藏键盘

图为我的应用程序的一部分,AutoCompleteTextView附带适配器.当用户在该视图中输入内容时,会显示自动填充建议.

我遇到的问题是:当显示建议并按下设备的向下箭头时,只有AutoCompleteTextView关闭的建议被关闭,键盘保持打开状态,需要再次点击向下箭头才能消失.

我确实希望第一次点击向下箭头时建议键盘消失.

我尝试覆盖onBackPressed但是当向下箭头被敲击时它没有被调用,大概是因为它不被认为是"后退".

我怎么能这样做?

编辑:我知道如何以编程方式隐藏键盘,我想我的问题是检测'向下箭头'点击.

在此输入图像描述

android

8
推荐指数
1
解决办法
1216
查看次数

使用Gson或Jackson对JSON进行反序列化时忽略空字段

我知道在将对象序列化为JSON时,有很多关于跳过具有空值的字段的问题.在将JSON反序列化为对象时,我想跳过/忽略具有空值的字段.

考虑上课

public class User {
    Long id = 42L;
    String name = "John";
}
Run Code Online (Sandbox Code Playgroud)

和JSON字符串

{"id":1,"name":null}
Run Code Online (Sandbox Code Playgroud)

做的时候

User user = gson.fromJson(json, User.class)
Run Code Online (Sandbox Code Playgroud)

我想 user.id成为'1'并user.name成为'约翰'.

Gson或Jackson是否可以采用一般方式(没有特殊TypeAdapter的或类似的)?

java json jackson gson deserialization

8
推荐指数
2
解决办法
4771
查看次数

当第一个项目的高度为 0 时,SwipeRefreshLayout 不起作用

SwipeRefreshLayout不起作用(动画未示出,onRefresh不叫)时在第一项RecyclerView中的SwipeRefreshLayout具有零高度。

你可以在 Github 上查看一个测试项目来展示这一点。

我的问题是:可以避免这种影响吗?在我的实际工程中,由于情况没有在我的手(广告库)我的清单中的第一项有时有高度0,所以将其设置为View.GONE或高度1是不是一种选择。

android swiperefreshlayout android-recyclerview

8
推荐指数
1
解决办法
474
查看次数

三星设备自动为共享Intent设置默认应用程序

我的应用程序中有一个功能,让人们分享内容.

它通常的工作方式是:

设备要求用户选择要处理的应用程序Intent.用户可以选择"只需一次"或"始终".

在一些三星设备上,例如Galaxy S6,缺少"一次性"和"总是"选项,在选择应用程序后,该应用程序成为该事件的标准.

这甚至会导致我的应用程序被新安装,在尝试共享时,根本不会询问用户,这Intent只是由用户在从其他应用程序共享时选择共享的应用程序处理的!

这里这里记录了一些三星设备的这个问题.

这是我构建我的简单意图的方式:

Intent intent = ShareCompat.IntentBuilder.from(this)
        .setSubject("mySubject")
        .setText("myText")
        .setType("text/plain")
        .setChooserTitle("myChooserTitle").getIntent();

startActivity(intent);
Run Code Online (Sandbox Code Playgroud)

我也尝试过:

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "myText");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "mySubject");
sendIntent.setType("text/plain");

startActivity(sendIntent);
Run Code Online (Sandbox Code Playgroud)

我的问题是:我可以从我的代码中做任何事情来防止这种情况,或者我必须告诉我的用户下次不要购买三星吗?

android android-intent samsung-mobile

8
推荐指数
1
解决办法
886
查看次数

显示带有声音的通知时出现FileUriExposedException

我的应用程序的用户可以选择在我的应用程序中收到通知时应播放的铃声。

我显示一个铃声选择对话框:

    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);

    startActivityForResult(intent, REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)

并保存结果:

    Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    PreferenceManager.getDefaultSharedPreferences(this).edit().putString(RINGTONE_SP_KEY, uri.toString()).apply();
Run Code Online (Sandbox Code Playgroud)

显示通知时,我使用用户选择的铃声或默认铃声:

private void showNotification() {
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setSound(getRingtone());
    builder.setContentText("MyContentText");
    builder.setSmallIcon(R.drawable.test);

    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, builder.build());
}

private Uri getRingtone() {

    String ringtoneUriFromSp = PreferenceManager.getDefaultSharedPreferences(this).getString(RINGTONE_SP_KEY, null);

    if (ringtoneUriFromSp == null) {
        return RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    } else {
        return Uri.parse(ringtoneUriFromSp);
    }
}
Run Code Online (Sandbox Code Playgroud)

默认和自定义选定的铃声Uri通常都看起来像这样:

content://media/internal/audio/media/16
Run Code Online (Sandbox Code Playgroud)

一切都很好。

我确实(很少)通过Crashlytics 收到以下异常

致命异常:android.os.FileUriExposedException file:///storage/emulated/0/MIUI/ringtone/CloudMagic_3.ogg通过Notification.sound在应用程序之外公开

适用于某些设备(例如Xiaomi Mi …

android android-notifications android-file

8
推荐指数
0
解决办法
979
查看次数

检查对象是否是任何"数字"类的实例?

Object o = ?
if ((o instanceof Integer) || (o instanceof Double) || (o instanceof Float)|| (o instanceof Long))
Run Code Online (Sandbox Code Playgroud)

是否有更短的版本来检查对象是否是任何数字类型?

java

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

Android Studio堆快照分析器 - "支配大小"代表什么?

在Android Studio中转储堆并查看创建的快照后,会出现一个名为"dominating size"的字段.它代表什么? 在此输入图像描述

android hprof android-studio

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

SwitchPreferenceCompat在添加到XML时以不同的方式在编程时创建

在a中PreferenceFragment,我SwitchPreferenceCompat添加了一个通过XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory android:title="cat 1">

        <SwitchPreferenceCompat
            android:key="pref_1"
            android:defaultValue="false"
            android:title="from xml"/>

    </PreferenceCategory>

    <PreferenceCategory
        android:key="pref_cat_1"
        android:title="cat 2"/>

</PreferenceScreen>
Run Code Online (Sandbox Code Playgroud)

和一个以编程方式添加:

PreferenceGroup preferenceGroup = (PreferenceGroup) findPreference("pref_cat_1");

SwitchPreferenceCompat switchPreference = new SwitchPreferenceCompat(getActivity());
switchPreference.setWidgetLayoutResource(android.support.v7.preference.R.layout.preference_widget_switch_compat);
switchPreference.setTitle("programmatically");
switchPreference.setChecked(true);
switchPreference.setDefaultValue(true);

switchPreference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
    @Override
    public boolean onPreferenceChange(final Preference preference, final Object newValue) {

        Toast.makeText(getActivity(), newValue.toString(), Toast.LENGTH_SHORT).show();

        return true;
    }
});

preferenceGroup.addPreference(switchPreference);
Run Code Online (Sandbox Code Playgroud)

在屏幕上,它们看起来不同(fontsize):

在此输入图像描述

我试着省略了这条线

switchPreference.setWidgetLayoutResource(android.support.v7.preference.R.layout.preference_widget_switch_compat);
Run Code Online (Sandbox Code Playgroud)

但是切换按钮变得不可见.

我怎样才能让它们看起来一样?

测试项目可以在Github上找到(branch_two).

android android-preferences android-support-library android-preference-v14

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

ConstraintLayout:将TextView放在ImageView下面

我面临着一个看似简单的任务:布局:

  • 一种ImageView根据图像的宽高比完全占据宽度,高度
  • TextView下面那个

这就是我所拥有的

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:adjustViewBounds="true"
        android:scaleType="fitXY"
        android:src="@drawable/hamster"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MyText MText …
Run Code Online (Sandbox Code Playgroud)

android android-layout android-constraintlayout

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

为什么不调用doOnDispose?

在创建像这样的Observable时:

public void foo() {

    Observable observable = Observable.fromCallable(() -> {
        bar();
        return "";
      })
      .doOnSubscribe(disposable -> System.out.println("onSubscribe"))
      .doOnDispose(() -> System.out.println("onDispose"));

    Disposable disposable = observable.subscribe();
    disposable.dispose();    
}

private void bar() {
    System.out.println("bar");
}
Run Code Online (Sandbox Code Playgroud)

doOnSubcribe叫,doOnDispose不叫.

这是为什么?

java rx-java2

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