【问题标题】:Update UI after app is created创建应用后更新 UI
【发布时间】:2014-06-06 07:23:47
【问题描述】:

我正在开发一个在其主要布局中具有滚动视图和图像按钮的 Android 应用。我使用 drawable 文件夹中的图像并通过它们的 id 调用它们。

//AboutScreen.java class
    private ImageButton mStartButton3;

    mStartButton3 = (ImageButton) findViewById(R.id.woops3); 

    mStartButton3.setOnClickListener(this);


//MainLayout.xml
    <ImageButton
        android:id="@+id/woops3"
        android:background="@android:color/transparent"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:gravity="center_horizontal|center_vertical"
        android:src="@drawable/woops6"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
             android:layout_marginTop="10dp"
        android:text="@string/button_start"
        android:textSize="@dimen/menu_title" />

但我想通过在启动画面期间从 json Web 服务下载的图像来更新这些图像(图像按钮)。

可以吗?

谢谢。

【问题讨论】:

    标签: android xml user-interface updates imagebutton


    【解决方案1】:

    去这个:

        public void onCreate(Bundle savedInstanceState) 
    
    { 
    
    Bitmap bitmap = DownloadImage("Your URL");
    
    ImageView img = (ImageView) findViewById(R.id.imagefromserver);
    
    img.setImageBitmap(bitmap);
    
    }
    
    
    private InputStream OpenHttpConnection(String urlString) 
        throws IOException
    
     {
    
     InputStream in = null;
    
            int response = -1;
    
            URL url = new URL(urlString); 
            URLConnection conn = url.openConnection();
    
            if (!(conn instanceof HttpURLConnection))                     
                throw new IOException("Not an HTTP connection");
    
            try{
                HttpURLConnection httpConn = (HttpURLConnection) conn;
                httpConn.setAllowUserInteraction(false);
                httpConn.setInstanceFollowRedirects(true);
                httpConn.setRequestMethod("GET");
                httpConn.connect(); 
    
                response = httpConn.getResponseCode();                 
                if (response == HttpURLConnection.HTTP_OK) {
                    in = httpConn.getInputStream();                                 
                }                     
            }
            catch (Exception ex)
            {
                throw new IOException("Error connecting");            
            }
            return in;     
        }
        private Bitmap DownloadImage(String URL)
        {        
            Bitmap bitmap = null;
            InputStream in = null;        
            try {
                in = OpenHttpConnection(URL);
                bitmap = BitmapFactory.decodeStream(in);
                in.close();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            return bitmap;                
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-07
      • 2021-10-27
      • 1970-01-01
      • 1970-01-01
      • 2013-03-21
      • 2020-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多