小编Hus*_*eky的帖子

CountDownTimer是否在Activity destroy()上自动取消?

一个问题,也许有点天真...

如果我在Activity onCreate()中启动2分钟的CountDownTimer并在用户单击按钮时使用finish()将其关闭,Android是否会自动取消它?

还是我需要在onDestroy()中显式调用cancel()?

android ondestroy android-activity countdowntimer

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

我可以通过编程方式更改强调色(AppCompat)吗?

我有:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="colorAccent">@color/myColor</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但我希望允许用户更改强调色.我可以用AppCompat做到吗?

android android-appcompat android-styles

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

Android-处理ExpandableListView高度

我在LinearLayout中实现了ExpandableListView,但是当我运行该应用程序时,得到的是: 在此处输入图片说明

首先,标题出现两次是否正常?

当单击上方的标题时,将得到展开的视图,即我希望的样子: 在此处输入图片说明

如果我触摸第二个标头(我认为不应该存在),它就会消失,让上一个标头单独出现(就像我希望的那样): 在此处输入图片说明

但是 可扩展高度已更改!: 在此处输入图片说明

我不知道为什么会这样。我只想有一个标题,并且我想固定大小。以下是带有相关代码的文件:

ExpandableListView所在的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_sv"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/white" >

    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="20dp" 
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp" >

        ...

        <ExpandableListView
            android:id="@+id/learnabletm"
            android:layout_width="match_parent"
            android:layout_height="200dp" />

    </LinearLayout>

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

expandable_list_view_child.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/child"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="17dip"
        android:paddingTop="5dp"
        android:paddingBottom="5dp"
        android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />

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

expandable_list_view_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="8dp">

    <TextView
        android:id="@+id/group"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
        android:textSize="18dp" />

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

ExpAdapter.java

public class ExpAdapter extends BaseExpandableListAdapter { …
Run Code Online (Sandbox Code Playgroud)

android expandablelistview expandablelistadapter

5
推荐指数
0
解决办法
2067
查看次数

即使应用关闭,如何在特定时间每天显示通知?

尽管Stack Overflow之前可能已经提出过这个问题,但我仍然没有找到明确的答案.

我想每天在中午12点显示通知,例如即使应用程序关闭也是如此.我从这些链接引用:在特定的时间通知,每天的Android,Android的日常使用,在一个AlarmManager每天的特定时间重复通知,Android的广播接收器在启动时-继续运行时,活动在背景和更多...我很困惑关于Service和BroadcastReceiver之间的区别.我应该使用哪一个?或者我应该同时使用它们?

到目前为止,我知道如何显示通知,但即使应用程序关闭,我也不知道如何每天自动显示一次.

我的代码:

public class NotifyService extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Toast.makeText(this, "Service created", Toast.LENGTH_LONG).show();

        Intent resultIntent = new Intent(this, HomeScreen.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);

        Notification.Builder notification = new Notification.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("App Title")
                .setContentText("Some Text...")
                .setContentIntent(resultPendingIntent);

        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        if (Build.VERSION.SDK_INT < 16) {
            notificationManager.notify(1, notification.getNotification());
        } else {
            notificationManager.notify(1, notification.build());
        }
    }

    @Override …
Run Code Online (Sandbox Code Playgroud)

android android-intent android-service android-alarms android-notifications

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

如何从android中的指定日期获取日期

假设我的日期是02-01-2013

它存储在一个变量中:

String strDate = "02-01-2013";
Run Code Online (Sandbox Code Playgroud)

那么我应该如何得到这个日期的那一天(即星期二)?

android date weekday dayofweek

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

在ActionBar中的菜单项上添加复选框

我的操作栏中有问题.我正在尝试在ActionBar中的菜单项上添加一个复选框,但它不起作用.它只显示复选框的标题.

这是我的menu.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="com.tracking.bus.maps.SingleViewMapsActivity" >

        <item
        android:id="@+id/actionbar_alarm"
        android:title="@string/actionbar_alarm"
        app:actionViewClass="android.widget.checkbox"
        app:showAsAction="ifRoom"/>
</menu>
Run Code Online (Sandbox Code Playgroud)

android android-actionbar android-checkbox

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

布局位于StatusBar和Soft Keys下

我不知道我是怎么做到这一点的,我找不到类似的东西,但我的软件导航和状态栏正在我的布局上绘制,而不是我的布局适合它们.

如何让我的布局在它们之间绘制而不是在?

例

编辑:

这似乎是罪魁祸首,位于风格:

    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">true</item>
Run Code Online (Sandbox Code Playgroud)

android translucency navigationbar android-layout android-statusbar

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

在活动的onPause()或onStop()状态下运行代码

我的Android应用程序中有一个功能,每次用户尝试编辑他或她的个人资料时都需要运行.在我的应用程序中有两个部分的编辑配置文件(请不要问为什么,它背后有一个非常漫长的繁琐原因).如果用户决定取消所有内容,我需要恢复用户在编辑配置文件的第一部分所做的更改.我在第二部分编辑个人资料发了取消按钮,但我的问题是,如果有什么用户按下返回按钮或设备上的HOME键和应用程序调用onPause()onStop()?如何在活动的这两个阶段运行相同的代码?那些知道如何将代码放在不同状态的活动的人吗?我只是创建一个函数onPause()并将代码粘贴在那里吗?那会有用吗?

android android-lifecycle onpause android-activity

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

如何在android中的字符串中的每个“\n”之后添加行间距?

\n正如问题所明确指出的那样,我想在 Android 资源文件中的字符串中的每个定义之后添加一个小行距。

假设我们在 xml 中定义了一个字符串,如下所示:

<string name="line_test">This is the 1st line. This is still the 1st line.\nThis is the 2nd line.\nThis is the 3rd line. This is still the 3rd line.\nThis is the 4th line.</string>
Run Code Online (Sandbox Code Playgroud)

将其设置为 a 时的正常输出TextView将类似于该图像的左侧。我想要实现的是该图像的右侧。

图1

xml中有一些属性,例如:

android:lineSpacingMultiplier="2"
Run Code Online (Sandbox Code Playgroud)

android:lineSpacingExtra="10dp"
Run Code Online (Sandbox Code Playgroud)

但这些属性只会导致这样的结果:

图2

所以我不想在每个新行之后添加行距,因为行的宽度被消耗,但仅在每个定义的\n.

有什么办法可以通过代码或html来实现我想要的吗?通过说 html,我的意思是类似这样的内容,但在添加后进行了一些修改<br/>

textView.setText(Html.fromHtml("This is the 1st line. This is still the 1st line.<br/>This is the 2nd line.<br/>This is the 3rd line. This is still the 3rd line.<br/>This is …
Run Code Online (Sandbox Code Playgroud)

html string android newline spacing

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

我怎么能循环几个按钮视图?

比如说我在我的XML文件中定义了几个按钮,并最初将它们设置为禁用.

<Button 
android:text="Off"
android:id="@+id/Button0"
android:enabled="false">
</Button>
<Button android:text="Off"
android:id="@+id/Button1"
android:enabled="false">
</Button>
<Button android:text="Off"
        android:id="@+id/Button2"
    android:enabled="false">
</Button>
<Button android:text="Off"
    android:id="@+id/Button3"
        android:enabled="false">
</Button>
Run Code Online (Sandbox Code Playgroud)

稍后在我的代码中我想要启用所有按钮,所以理想情况下这应该在循环中完成,但我无法弄清楚如何在循环中访问按钮.例如,我不想提出几个陈述:

Button b = (Button)findViewById(R.id.Button0);
/* Do some action on button0 */
b = (Button)findViewById(R.id.Button1);
/* Do some action on button1 */
/* And continue with explicit statements for each button*/
Run Code Online (Sandbox Code Playgroud)

那么如何在循环中实现上述目标呢?

android for-loop android-button

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