小编Zie*_*iem的帖子

如何从Android中的PreferenceActivity获取SharedPreferences?

我正在使用PreferenceActivity为我的应用程序显示一些设置.我通过xml文件膨胀设置,以便我的onCreate(和完整的类方法)如下所示:

public class FooActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        addPreferencesFromResource(R.xml.preference);
    }
}
Run Code Online (Sandbox Code Playgroud)

PreferenceActivity PreferenceFragment的javadoc 说明了这一点

当用户与它们交互时,这些首选项将自动保存到SharedPreferences.要检索此活动中的首选项层次结构将使用的SharedPreferences实例,请使用与此活动相同的包中的上下文调用getDefaultSharedPreferences(android.content.Context).

但是我如何在另一个Activity中获得SharedPreference的名称?我只能打电话

getSharedPreferences(name, mode)
Run Code Online (Sandbox Code Playgroud)

在其他活动中但我需要PreferenceActivity使用的SharedPreference的名称.名称是什么或如何检索它?

android preferences preferenceactivity sharedpreferences

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

以编程方式在LinearLayout中设置边距

我正在尝试使用Java(而不是XML)创建一个LinearLayout,其中的按钮可以填充屏幕并具有边距.这是没有边距的代码:

LinearLayout buttonsView = new LinearLayout(this);
buttonsView.setOrientation(LinearLayout.VERTICAL);
for (int r = 0; r < 6; ++r) {
    Button btn = new Button(this);
    btn.setText("A");

    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); // Verbose!
    lp.weight = 1.0f; // This is critical. Doesn't work without it.
    buttonsView.addView(btn, lp);
}

ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
setContentView(buttonsView, lp);
Run Code Online (Sandbox Code Playgroud)

这样工作正常,但你怎么给按钮边距,所以它们之间有空间?我尝试过使用LinearLayout.MarginLayoutParams,但是没有weight成员,所以没有用.如果你lp在它的构造函数中传递它也不起作用.

这不可能吗?因为它确实看起来,并且它不会是第一个Android布局任务,你只能用XML做.

java layout android margin view

260
推荐指数
8
解决办法
33万
查看次数

如何初始化Kotlin的MutableList以清空MutableList?

看起来很简单,但是,如何将Kotlin初始化MutableList为空MutableList

我可以通过这种方式破解它,但我确信有更容易的东西:

var pusta: List<Kolory> = emptyList()
var cos: MutableList<Kolory> = pusta.toArrayList()
Run Code Online (Sandbox Code Playgroud)

kotlin

210
推荐指数
4
解决办法
9万
查看次数

淡入淡出Java中的Android动画

我希望有一个2秒的ImageView动画,花费1000毫秒渐弱,然后1000毫秒淡出.

这是我在ImageView构造函数中到目前为止的内容:

Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);

Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);

AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
this.setAnimation(animation);
Run Code Online (Sandbox Code Playgroud)

当我运行该动画时,没有任何显示.但是,当我删除其中一个alpha动画时,行为按预期工作.

我已经尝试过的事情:

  • 的所有可能的组合setFillBefore,setFillAftersetFillEnabled.
  • 添加LinearInterpolatorAnimationSet.

animation android

141
推荐指数
9
解决办法
20万
查看次数

Android ListView标头

我有ListView,它有一些事件.事件按日排序,我希望每天都有标题日期,然后在下面收听事件.

以下是我填充该列表的方式:

ArrayList<TwoText> crs = new ArrayList<TwoText>();

crs.add(new TwoText("This will be header", event.getDate()));

for (Event event : events) {
    crs.add(new TwoText(event.getStartString() + "-" + event.getEndString(), event.getSubject()));
}

arrayAdapter = new TwoTextArrayAdapter(this, R.layout.my_list_item, crs);
lv1.setAdapter(arrayAdapter);
Run Code Online (Sandbox Code Playgroud)

这就是我的类TwoText看起来的样子:

public class TwoText {
    public String classID;
    public String state;

    public TwoText(String classID, String state) {
        this.classID = classID;
        this.state = state;
    }
}
Run Code Online (Sandbox Code Playgroud)

这就是我的TwoTextArrayAdapter类的外观:

import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class TwoTextArrayAdapter extends …
Run Code Online (Sandbox Code Playgroud)

java android listview header android-arrayadapter

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

如何检查某个活动是否可以处理意图?

到目前为止,我有这种方法,但它似乎缺少了一些东西

例如,我有一个文件/sdcard/sound.3ga返回false(就像没有可以处理这种类型文件的活动),但当我从文件管理器打开它时,它打开媒体播放器没有问题

我认为这个意图并不完整,我需要更多的东西让我自己确定只有没有可以处理这个意图的活动时,handlerExists变量才会为false

PackageManager pm = getPackageManager();
Intent intent = new Intent(android.content.Intent.ACTION_VIEW);
String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uriString)).toString());
String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent.setDataAndType(Uri.fromFile(new File(uriString)),mimetype);
boolean handlerExists = intent.resolveActivity(pm) != null;
Run Code Online (Sandbox Code Playgroud)

android file-type intentfilter android-intent mime-types

105
推荐指数
4
解决办法
5万
查看次数

TextInputLayout在用户关注它之前没有显示EditText提示

我正在使用最近发布的Android设计支持库来显示带有EditTexts的浮动标签.但我面临的问题是,在渲染UI时,EditText上的提示没有显示,但是在我专注于EditTexts之后我看到了Hint.

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <ScrollView
        android:id="@+id/ScrollView01"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingLeft="@dimen/activity_horizontal_margin"
            android:paddingRight="@dimen/activity_horizontal_margin">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/name_et_textinputlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin">

                <EditText
                    android:id="@+id/FeedBackerNameET"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/feedbackname"
                    android:inputType="textPersonName|textCapWords" />
            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:id="@+id/email_textinputlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/FeedBackerEmailET"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/feedbackemail"
                    android:inputType="textEmailAddress" />

            </android.support.design.widget.TextInputLayout>

            <Spinner
                android:id="@+id/SpinnerFeedbackType"
                android:layout_width="fill_parent"
                android:layout_height="48dp"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:entries="@array/feedbacktypelist"
                android:prompt="@string/feedbacktype" />

            <android.support.design.widget.TextInputLayout
                android:id="@+id/body_textinputlayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <EditText
                    android:id="@+id/EditTextFeedbackBody"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:hint="@string/feedbackbody"
                    android:inputType="textMultiLine|textCapSentences"
                    android:lines="5" />

            </android.support.design.widget.TextInputLayout>

            <CheckBox
                android:id="@+id/CheckBoxFeedBackResponse"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/activity_vertical_margin"
                android:text="@string/feedbackresponse" />

            <Button
                android:id="@+id/ButtonSendFeedback" …
Run Code Online (Sandbox Code Playgroud)

android material-design android-design-library

94
推荐指数
5
解决办法
4万
查看次数

排球 - POST/GET参数

我看到关于Volley的Google IO 2013会议,我正在考虑转向凌空.Volley是否支持添加POST/GET参数来请求?如果是,我该怎么办?

networking android android-volley

76
推荐指数
5
解决办法
16万
查看次数

清单中的权限在Android 6中不起作用

它完全忽略了:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Run Code Online (Sandbox Code Playgroud)

所以我得到了例外:

引起:android.view.WindowManager $ BadTokenException:无法添加窗口android.view.ViewRootImpl$W@86fb55b - 此窗口类型的权限被拒绝

它甚至没有列出:

在此输入图像描述

我该如何解决?谢谢.

编辑:

它列在配置应用程序/高级/绘制其他应用程序中.所以我打开它现在它工作正常,但为什么在我运行我的应用程序时没有任何对话框询问权限?所有的perrmissions被deafult关闭,我需要去设置并mannualy打开它?

android android-6.0-marshmallow

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

从后堆栈中删除片段

当平板电脑被纵向握持时,我在活动中有3个片段.但是在景观中我只有2个这样的片段.我遇到的问题是当从纵向到横向时,活动正在创建第3个片段.我收到并收到错误,因为无法创建此片段.

我已经知道这个片段正在创建,因为它位于后栈中.

我试图通过使用删除onDestroy方法中的片段

FragmentTransaction f = fragmentManager.beginTransaction();
f.remove(mf);
f.commit();
Run Code Online (Sandbox Code Playgroud)

但是我得到一个错误,说我不能在onSaveInstanceState之后使用这个函数

将这个片段从后栈中取出的正确方法是什么?

更新

我应该补充一点,我遇到问题的片段是来自这个库的mapFragment

https://github.com/petedoyle/android-support-v4-googlemaps

我使用它的方式就是这样

mf = MapFragment.newInstance(1, true);

ft = fragmentManager.beginTransaction();
ft.replace(R.id.mapContainer, mf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack("map");
ft.commit();
Run Code Online (Sandbox Code Playgroud)

android android-fragments

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