小编Squ*_*onk的帖子

Java使用带有switch语句的枚举

我已经在SO上查看了类似于这个问题的各种问答,但还没有找到解决方案.

我所拥有的是一个枚举,它代表了观看电视指南的不同方式......

在NDroid Application类中

static enum guideView {
    GUIDE_VIEW_SEVEN_DAY,
    GUIDE_VIEW_NOW_SHOWING,
    GUIDE_VIEW_ALL_TIMESLOTS
}
Run Code Online (Sandbox Code Playgroud)

...当用户更改视图时,事件处理程序int从0-2 接收,我想做这样的事情......

在Android Activity onClick(DialogInterface dialog, int which)事件处理程序中

// 'which' is an int from 0-2
switch (which) {
    case NDroid.guideView.GUIDE_VIEW_SEVEN_DAY:
    ...
    break;
}
Run Code Online (Sandbox Code Playgroud)

我已经习惯了C#enums和select/case语句,这些语句允许类似上面的内容,我知道Java做的事情有所不同,但我无法理解我需要做什么.

我不得不诉诸if陈述吗?可能只有3个选择,所以我可以做到但我想知道如何用Java中的switch-case完成它.

编辑对不起我没有完全扩展这个问题,因为我认为它是一个通用的Java问题.我已经添加了这个问题来进一步解释.

没有任何Android特定的东西,这就是为什么我没有将它标记为Android,但枚举是在Application类和我不想切换的代码中定义的Activity.枚举是静态的,因为我需要从多个活动中访问它.

java enums switch-statement

103
推荐指数
4
解决办法
24万
查看次数

Android中的MD5哈希

我有一个简单的Android客户端,需要与简单的C#HTTP侦听器"对话".我想通过在POST请求中传递用户名/密码来提供基本级别的身份验证.

MD5哈希在C#中是微不足道的,并且为我的需求提供了足够的安全性,但我似乎无法在android端找到如何做到这一点.

编辑:只是为了解决有关MD5弱点的问题 - C#服务器运行在我的Android客户端用户的PC上.在许多情况下,他们将使用自己的局域网上的Wi-Fi访问服务器,但他们可能会自己冒险从互联网上访问它.此外,服务器上的服务需要使用MD5的传递到我无法控制的第三方应用程序.

android md5 cryptography

86
推荐指数
7
解决办法
13万
查看次数

将主题应用于Android中的活动?

我知道如何将主题应用于整个应用程序,但是我将在哪里将主题应用于单个活动?

android themes manifest

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

将UI元素的位置/大小设置为屏幕大小的百分比

我正在努力研究在创建布局时是否可以使用百分比位置/大小.我想要的是这样的......

^
|
|
| 68%
|
|
v
Gallery (height equivalent of 16% total screen size)
^
| 16%
v
Run Code Online (Sandbox Code Playgroud)

我正在测试一个设备,它在横向上有800x480实际像素的显示,我正在用以下强制它...

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" > 
    <Gallery 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="80px"
        android:layout_marginTop ="320px"
    />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

很显然,我不想硬编码固定px装置,但我不能使用68%0.68用于layout_marginTop(例如).我看过dp单位,但我不确定我是否能这样做.

我不得不承认UI设计是我的一个弱点,所以任何建议都会感激不尽.

编辑:为了将来的参考,如果有人正在寻找类似的答案,遵循艾伦摩尔的建议,我有以下工作正是我想要的...

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="@drawable/bground"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.68"
        android:background="@android:color/transparent"
    />
    <Gallery 
        android:id="@+id/gallery"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.16"
    />
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="0.16" 
        android:background="@android:color/transparent"
    /> …
Run Code Online (Sandbox Code Playgroud)

android android-layout

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

更新Android应用(不使用Google Play)

我写了一个应用程序的Beta版本.它可以通过网络下载(我不会将其发布到Play Market).在新版本发布时,是否可以在没有Play Market的情况下更新此应用程序?

android

55
推荐指数
4
解决办法
8万
查看次数

Android - 通知管理器,没有意图的通知

我希望能够发出通知以提醒用户有关已完成的计时器,但是当您单击通知时我不希望有意图.

我试过传入null的意图

String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(ns);

int icon = R.drawable.icon;
CharSequence tickerText = "Hello";
long when = System.currentTimeMillis();

Notification notification = new Notification(icon, tickerText, when);

CharSequence contentTitle = "My notification";
CharSequence contentText = "Hello World!";

notification.setLatestEventInfo(context, contentTitle, contentText, null);
mNotificationManager.notify(HELLO_ID, notification);
Run Code Online (Sandbox Code Playgroud)

notifications android android-intent notificationmanager

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

具有完全限定的DB路径名的SQLiteOpenHelper问题

在我的应用程序中,我使用...

myFilesDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                      + "/Android/data/" + packageName + "/files");
myFilesDir.mkdirs();
Run Code Online (Sandbox Code Playgroud)

这很好,结果路径是......

/mnt/sdcard/Android/data/com.mycompany.myApp/files
Run Code Online (Sandbox Code Playgroud)

我需要一个我想要存储在SD卡上的SQLite DB,所以我将SQLiteOpenHelper扩展如下......

public class myDbHelper extends SQLiteOpenHelper {

    public myDbHelper(Context context, String name, CursorFactory factory, int version) {
        // NOTE I prefix the full path of my files directory to 'name'
        super(context, myFilesDir + "/" + name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // Create tables and populate with default data...
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止一切顺利 - 我第一次打电话getReadableDatabase()getWriteableDatabase()在SD卡上创建空DB并onCreate()填充它.

所以这就是问题 …

android

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

片段getArguments()返回null

我有一个Fragment具有TabHost作为根布局如下...

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <FrameLayout
                android:id="@+id/tab_1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

            <!-- More FrameLayouts here - each are placeholders for Fragments -->    

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

Fragment为选项卡内容创建/更新每个代码的代码如下...

private void updateTab(String tabId, int placeholder) {
    FragmentManager fm = getFragmentManager();
    if (fm.findFragmentByTag(tabId) == null) {
        Bundle arguments = new Bundle();
        arguments.putInt("current_day", mCurrentTab);
        EpgEventListFragment fragment = new EpgEventListFragment();
        fragment.setArguments(arguments);

        fm.beginTransaction()
                .replace(placeholder, new EpgEventListFragment(), …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

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

Java android:使用TextView附加换行符

我只是想以某种方式在我的线性布局中添加一个新行:

layout = (LinearLayout) findViewById (R.id.layout);  

... //some other code where I've appended some strings already

final TextView nline = new TextView(this);
nline.setText(Html.fromHtml("<br>")); //i also tried:  nline.setText("\n");
layout.addView(nline);
Run Code Online (Sandbox Code Playgroud)

但这只是增加了几个空格.有人可以帮我吗?谢谢.

java android newline textview

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

修改SimpleCursorAdapter的数据

我正在制作一个电视指南应用程序,它使用一次ListActivity显示一个频道/一天的电视节目.我使用RelativeLayoutListView项目,我想ListView看看是这样的:

07:00 The Breakfast Show
      Latest news and topical reports
08:00 Tom and Jerry
      More cat and mouse capers
Run Code Online (Sandbox Code Playgroud)

ListView使用以下代码获取项目的数据:

Cursor cursor = db.rawQuery(SELECT blah,blah,blah);
String[] columnNames = new String[]{"start_time","title", "subtitle"};
int[] resIds = new int[]{R.id.start_time_short, R.id.title, R.id.subtitle};
adapter = new SimpleCursorAdapter(this, R.layout.guide_list_item, cursor, columnNames, resIds);
Run Code Online (Sandbox Code Playgroud)

我的问题是该start_time字段datetime具有以下格式:

2011-01-23 07:00:00
Run Code Online (Sandbox Code Playgroud)

所以我得到的是:

2011-01-23 07:00:00 The Breakfast Show
                    Latest news and topical reports
2011-01-23 08:00:00 Tom and Jerry
                    More …
Run Code Online (Sandbox Code Playgroud)

android android-listview simplecursoradapter android-viewbinder

16
推荐指数
1
解决办法
4885
查看次数