小编Hub*_*ert的帖子

如何在String的结尾和/或开头保留空格?

我必须从我的资源/值文件连接这两个字符串:

<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on </string>
<string name="Toast_Memory_GameWon_part2"> flips !</string>
Run Code Online (Sandbox Code Playgroud)

我是这样做的:

String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+total_flips+getString(R.string.Toast_Memory_GameWon_part2);

Toast.makeText(this, message_all_pairs_found, 1000).show();
Run Code Online (Sandbox Code Playgroud)

但是第一个字符串末尾和第二个字符串开头的空格已经消失(当显示Toast时)......

我该怎么办 ?

我想答案就在这个文档链接的某处

或者它是否像使用&amp ;"&"字符?

android string-concatenation

334
推荐指数
9
解决办法
14万
查看次数

如何在Android中暂停/休眠线程或进程?

我想在两行代码之间暂停一下,让我解释一下:

- >用户单击一个按钮(实际上是一张卡片),我通过更改此按钮的背景来显示它:

thisbutton.setBackgroundResource(R.drawable.icon);
Run Code Online (Sandbox Code Playgroud)

- >让我们说1秒后,我需要通过改变它的背景来回到按钮的先前状态:

thisbutton.setBackgroundResource(R.drawable.defaultcard);
Run Code Online (Sandbox Code Playgroud)

- >我试图在这两行代码之间暂停线程:

try {
    Thread.sleep(1000);
} catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

但是,这不起作用.也许这是我需要暂停的过程而不是线程?

我也尝试过(但它不起作用):

new Reminder(5);
Run Code Online (Sandbox Code Playgroud)

有了这个:

public class Reminder {

Timer timer;

        public Reminder(int seconds) {
            timer = new Timer();
            timer.schedule(new RemindTask(), seconds*1000);
        }

        class RemindTask extends TimerTask {
            public void run() {
                System.out.format("Time's up!%n");
                timer.cancel(); //Terminate the timer thread
            }
        }  
    }
Run Code Online (Sandbox Code Playgroud)

如何暂停/休眠线程或进程?

multithreading android process

284
推荐指数
9
解决办法
58万
查看次数

随机改组阵列

我需要随机调整以下数组:

int[] solutionArray = {1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1};
Run Code Online (Sandbox Code Playgroud)

这有什么功能吗?

java arrays shuffle

214
推荐指数
10
解决办法
42万
查看次数

在应用程序内更改区域设置

我的用户可以更改应用内的区域设置(他们可能希望将手机设置保留为英文,但可以用法语,荷兰语或任何其他语言阅读我的应用内容...)

为什么这个在1.5/1.6中完全正常但在2.0中不再是???

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case 201:
        Locale locale2 = new Locale("fr"); 
        Locale.setDefault(locale2);
        Configuration config2 = new Configuration();
        config2.locale = locale2;
        getBaseContext().getResources().updateConfiguration(
            config2, getBaseContext().getResources().getDisplayMetrics());
        // loading data ...
        refresh();
        // refresh the tabs and their content
        refresh_Tab ();   
     break;
     case 201: etc...
Run Code Online (Sandbox Code Playgroud)

问题是,每当用户通过以上代码行时,MENU"缩小"越来越多......

这是缩小的菜单:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 100, 1, "REFRESH").setIcon(android.R.drawable.ic_menu_compass);
    SubMenu langMenu = menu.addSubMenu(0, 200, 2, "NL-FR").setIcon(android.R.drawable.ic_menu_rotate);
        langMenu.add(1, 201, 0, "Nederlands");
        langMenu.add(1, 202, 0, "Français");
    menu.add(0, 250, 4, R.string.OptionMenu2).setIcon(android.R.drawable.ic_menu_send);
    menu.add(0, 300, …
Run Code Online (Sandbox Code Playgroud)

android locale menu

195
推荐指数
5
解决办法
15万
查看次数

Android:使用带有setOnClickListener/onClick的SWITCH语句超过1个按钮?

假设我在LinearLayout中有几个按钮,其中2个是:

mycards_button = ((Button)this.findViewById(R.id.Button_MyCards));
exit_button = ((Button)this.findViewById(R.id.Button_Exit));
Run Code Online (Sandbox Code Playgroud)

我注册setOnClickListener()了他们两个:

mycards_button.setOnClickListener(this);
exit_button.setOnClickListener(this);
Run Code Online (Sandbox Code Playgroud)

如何使SWITCH区分Onclick中的两个按钮?

public void onClick(View v) {
  switch(?????){
    case ???:
      /** Start a new Activity MyCards.java */
      Intent intent = new Intent(this, MyCards.class);
      this.startActivity(intent);
      break;
    case ???:
      /** AlerDialog when click on Exit */
      MyAlertDialog();
      break;
}
Run Code Online (Sandbox Code Playgroud)

android button listener

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

Android:使用Asynctask从Web加载图像

如何用Asynctask替换以下代码行?你如何从Asynctask"取回"位图?谢谢.

ImageView mChart = (ImageView) findViewById(R.id.Chart);
String URL = "http://www...anything ...";

mChart.setImageBitmap(download_Image(URL));

public static Bitmap download_Image(String url) {

        //---------------------------------------------------
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {
            Log.e("Hub","Error getting the image from server : " + e.getMessage().toString());
        } 
        return bm;
        //---------------------------------------------------

    }
Run Code Online (Sandbox Code Playgroud)

我想过这样的事情:

替换:

mChart.setImageBitmap(download_Image(graph_URL));
Run Code Online (Sandbox Code Playgroud)

通过类似的东西:

mChart.setImageBitmap(new DownloadImagesTask().execute(graph_URL));
Run Code Online (Sandbox Code Playgroud)

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

android android-asynctask

36
推荐指数
2
解决办法
9万
查看次数

将垂直滚动条添加到Android中的AlertDialog?

我想在AlertDialog中添加一个垂直滚动条,因为我的文字太长而无法在1个屏幕上显示:

我试过用:

android:scrollbars="vertical" 
android:scrollbarAlwaysDrawVerticalTrack="true"
Run Code Online (Sandbox Code Playgroud)

但滚动条甚至不显示?

这是我正在使用的xml布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:scrollbarAlwaysDrawVerticalTrack="true"
    android:id="@+id/instructions_view" >
    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A LONG TEXT 1"/>

    <TextView 
        android:id="@+id/TextView02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="A LONG TEXT 2"/>

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

我用以下方法调用AlertsDialog:

public void onClick(View v) {
  switch(v.getId()){

    case R.id.Button_Instructions: 
     InstructionsDialog();
    break;

    case R.id.Button_Exit: 
     ExitDialog();
    break;
    }
 }

public void InstructionsDialog(){

  AlertDialog.Builder ad = new AlertDialog.Builder(this);
  ad.setIcon(R.drawable.icon);
  ad.setTitle("Instructions ...");
  ad.setView(LayoutInflater.from(this).inflate(R.layout.instructions_dialog,null));

  ad.setPositiveButton("OK", 
    new android.content.DialogInterface.OnClickListener() {
     public void onClick(DialogInterface dialog, int arg1) {
      // OK, go back to Main menu
     } …
Run Code Online (Sandbox Code Playgroud)

android dialog scrollbar

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

ANDROID:如果WiFi已启用且处于活动状态,则启动意图

这就是我想做的事情:

=>如果WiFi已启用且处于活动状态,则启动一个意图(实际上它是一个获取其内容的WebView =>我在网络上的应用程序的说明)

=>如果不是,那么我会启动另一个意图,这样我就不会显示WebView"Web页面不可用... http://www.mywebsite.com上的网页可能暂时关闭或者可能有感动..."

我最初打算使用

如果(wifi.isWifiEnabled())

但这并没有说明Wifi连接是否为ACTIVE.它只表示用户已打开开关.设备可能连接也可能没有连接......这是正确的吗?

然后我试着用:

if(wifi.getConnectionInfo().getSSID()!= null)

但我注意到它返回一个字符串,即使连接已丢失或被禁用...?

那我该怎么办?

wifi = (WifiManager)getSystemService(Context.WIFI_SERVICE);
Intent intent_instructions;

            if (wifi.getConnectionInfo().getSSID()!= null){
                Log.i("Hub", "WiFi is enabled AND active !");
                Log.i("Hub", "SSID = "+wifi.getConnectionInfo().getSSID());
                intent_instructions = new Intent(this, Instructions.class);
            }else{
                Log.i("Hub", "NO WiFi");
                intent_instructions = new Intent(this, Instructions_No_WiFi.class);
            }
            this.startActivity(intent_instructions);
Run Code Online (Sandbox Code Playgroud)

是否有更通用的方法来测试设备是否在启动意图之前连接到互联网?无论是通过Wifi,3G等...

在此先感谢您的帮助.

android internet-connection wifi

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

ANDROID:将屏幕拆分为2等于具有2个列表视图的部分

我正在尝试将2个列表视图放入我的布局中.问题是我不知道每个listview的大小.第一个listview可能有几个项目(0,1,2到大约10),第二个listview可能有很多项目(最多100个).

我试图将两个listviews的权重设置为1,但它不起作用:

=>如果第一个列表视图只有1个项目,第二个列表视图99,那么你看不到列表视图的第一项#1 =>它会缩小(相对于列表视图#2)你没有看到它.

所以我现在想把屏幕分成2个等于部分(无论是什么/无论每个列表视图的大小),并将两个列表视图放在每个部分中.当然它需要在任何设备上工作...所以如何捕获设备屏幕大小,将其分成两部分并强制列表视图大小适合屏幕的每一半?

有没有人这样做过?有另一种选项,以显示在相同的布局两个不同尺寸的列表视图(I应该使用滚动型以某种方式?=>当用户到达第一列表视图的结束时,第二列表视图显示=>是可能的? )

感谢您的帮助和任何建议......

休伯特

android listview android-layout

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

AlarmManager和BroadcastReceiver而不是Service - 那很糟糕吗?(超时)

背景信息:

我需要大约每小时更新一些来自网络的数据,即使我的应用程序关闭也是如此.更新数据本身大约需要40秒到1分钟.然后将其作为Serializable保存到文件中.我的应用启动时会读取此文件.

这是我为时刻而采取的方法(不使用服务)

像这样使用AlarmManager和BroadcastReceiver:

private void set_REFRESH_DATA_Alarm(){
    mContext = Main.this;
    alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    broadcast_intent = new Intent(mContext, 
            RepeatingAlarmReceiver_REFRESH_DATA.class);
    pendingIntent = PendingIntent.getBroadcast(mContext, 0,  broadcast_intent, 0);
    // do a REFRESH every hour, starting for the first time in 30 minutes from now ...
    Calendar now = Calendar.getInstance();
    long triggerAtTime = now.getTimeInMillis()+ (1 * 30 * 60 * 1000); // starts in 30 minutes
    long repeat_alarm_every = (1 * 60 * 60 * 1000); // repeat every 60 minutes
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtTime, 
            repeat_alarm_every, …
Run Code Online (Sandbox Code Playgroud)

service android broadcastreceiver alarmmanager

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

我可以在我的免费 Android 应用程序和小部件中接受比特币提示或捐赠吗?

谷歌是否正式允许(或至少容忍)这样做?

我有一些用户向我建议,添加一个点击“捐赠按钮”可能是一个好主意,打开他们默认的 Android 比特币钱包应用程序,并预先填写我的比特币地址。但是,嘿,我不想看到我的应用程序因此被暂停!

您是否在 Google Play 上看到过一些应用程序或小部件这样做?

我猜可能会问有关 Paypal 捐款的相同问题...

感谢您分享您的意见。

android paypal bitcoin

6
推荐指数
1
解决办法
1811
查看次数

如何将随机标记添加到地图但避开大海?

Google Ajax API游乐场(http://code.google.com/apis/ajax/playground/?exp=maps#map_markers)提供了一个很好的例子,可以将随机标记添加到任何地图:

function initialize() {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(37.4419, -122.1419), 9);

    // Add 10 markers to the map at random locations
    var bounds = map.getBounds();
    var southWest = bounds.getSouthWest();
    var northEast = bounds.getNorthEast();
    var lngSpan = northEast.lng() - southWest.lng();
    var latSpan = northEast.lat() - southWest.lat();
    for (var i = 0; i < 10; i++) {
      var point = new GLatLng(southWest.lat() + latSpan * Math.random(),
                              southWest.lng() + lngSpan * Math.random());
      map.addOverlay(new GMarker(point)); …
Run Code Online (Sandbox Code Playgroud)

google-maps

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

Android:你会如何让Buttons在一段时间内无法点击?

我有一些我想要无法点击的按钮(但仍然出现在屏幕上),直到另一个进程(线程)完成其工作?你会怎么做?

目标是避免我的用户继续点击此按钮,因此,每次点击它时都会启动一个新线程...

android onclick button

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