Android的 - 从意向额外的ImageView的传递变量 [英] android - passing variable from intent extra to ImageView

查看:82
本文介绍了Android的 - 从意向额外的ImageView的传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里Android的新人。我试图建立一个活动来显示所选图像。这code的基础是从您好,Android的第3版(程序员修炼),我试图修改。

基本上,我希望能够点击在一个活动的一个按钮,并具有启动另一个改变的布局,以显示与该按钮相关联的图像的活动。我有几个按钮,并希望每一个按钮,导致显示不同的图像。

文件(简体$除去PIC3 C $ç-...):

main.xml中:对于两个按钮(PIC1和PIC2)布局

Main.java:包含onclicklisteners的按钮 - 意图瓦特/演员(文件名映像)

Viewer.java:违约日食创建的,目的是建立新的布局的setContentView(R.layout.viewer);

viewer.xml:为ImageView的布局

我已经得到存储在strings.xml中工作(我创建一个字符串在Eclipse中使用@可绘制/ PIC1的价值,并给它imagename的名字,所以我可以叫@字符串/ imagename为ImageView的在SRC字符串viewer.xml)。

不过,我已经从搜索和阅读关于这个论坛,我不能一个活动中更改从strings.xml中值教训(我最初的想法是有code在Viewer.java将改变两行imagename字符串无论是通过了意向花絮。

我发现这个职位(http://stackoverflow.com/questions/3523384/android-pass-string-from-activity-to-layout)如果有人试图做类似的事情有一个TextView,但我已经尝试过这条路线,我不断在这些线路越来越语法错误。

我真的卡住。有任何想法吗? 谢谢!

main.java

 公共类主要活动扩展实现OnClickListener {

@覆盖
公共无效的onCreate(包savedInstanceState){
   super.onCreate(savedInstanceState);
   的setContentView(R.layout.main);


//点​​击监听器
查看pic1Button = findViewById(R.id.pic1_button);
pic1Button.setOnClickListener(本);
查看pic2Button = findViewById(R.id.pic2_button);
pic2Button.setOnClickListener(本);

}

// ...
公共无效的onClick(视图v){
开关(v.getId()){

   案例R.id.pic1_button:
      意图L =新的意图(这一点,Viewer.class);
      l.putExtra(映像文件名称,pic1filename);
      startActivity(升);
      打破;

   案例R.id.pic2_button:
      意图I =新的意图(这一点,Viewer.class);
      i.putExtra(映像文件名称,pic2filename);
      startActivity(ⅰ);
      打破;

}
}
}
 

viewer.xml

 < XML版本=1.0编码=UTF-8&GT?;

<的FrameLayout
   的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
   机器人:layout_width =FILL_PARENT
   机器人:layout_height =FILL_PARENT>
   < ImageView的机器人:ID =@ + ID / ImageView的
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =FILL_PARENT
      机器人:SRC =@字符串/ imagename
      机器人:scaleType =矩阵>
   < / ImageView的>

< /的FrameLayout>
 

解决方案

你的问题不是很清楚,但是从我的理解,你可以做到这一点用下面的code。

AndroidManifest.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
      包=com.stackoverflow.biowi
      安卓版code =1
      机器人:VERSIONNAME =1.0>
     <应用机器人:标签=@字符串/ APP_NAME机器人:图标=@可绘制/图标>
        <活动机器人:名称=主
                  机器人:标签=@字符串/ APP_NAME>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动机器人:名称=浏览器/>
    < /用途>
< /舱单>
 

Main.java

 包com.stackoverflow.biowi;

进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;

公共类主要活动扩展实现OnClickListener
{
    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.main);

        //点​​击监听器
        查看pic1Button = findViewById(R.id.pic1_button);
        pic1Button.setOnClickListener(本);
        查看pic2Button = findViewById(R.id.pic2_button);
        pic2Button.setOnClickListener(本);
    }

    公共无效的onClick(视图v){

        意图I =新的意图(这一点,Viewer.class);

        开关(v.getId()){
        案例R.id.pic1_button:
            i.putExtra(映像文件名称,pic1filename);
            打破;
        案例R.id.pic2_button:
            i.putExtra(映像文件名称,pic2filename);
            打破;
         }
        startActivity(ⅰ);
    }
}
 

Viewer.java:

 包com.stackoverflow.biowi;

进口android.app.Activity;
进口android.os.Bundle;
进口android.widget.ImageView;
进口android.database.Cursor;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.view.View;
进口android.widget.ImageView;

公共类浏览器扩展活动
{

    / **要记录在案。 * /
    私人位图mImage1;
    / **要记录在案。 * /
    私人位图mImage2;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.viewer);

        束束= getIntent()getExtras()。
        串imageName = bundle.getString(映像文件名称);


        //点​​击监听器
        ImageView的ImageView的=(ImageView的)findViewById(R.id.imageView);

        mImage1 = BitmapFactory.de codeResource(getResources(),R.drawable.pic1);
        mImage2 = BitmapFactory.de codeResource(getResources(),R.drawable.pic2);

        如果(imageName.matches(pic1filename)){
            imageView.setImageBitmap(mImage1);
        }否则,如果(imageName.matches(pic2filename)){
            imageView.setImageBitmap(mImage2);
        }
    }

}
 

main.xml中:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
              机器人:方向=垂直
              机器人:layout_width =FILL_PARENT
              机器人:layout_height =FILL_PARENT
              >
  <的TextView
      机器人:layout_width =FILL_PARENT
      机器人:layout_height =WRAP_CONTENT
      机器人:文字=世界您好,主营
      />
  <按钮机器人:ID =@ + ID / pic1_button
          机器人:layout_width =WRAP_CONTENT
          机器人:layout_height =WRAP_CONTENT
          机器人:文本=你好,我是一个按钮/>
  <按钮机器人:ID =@ + ID / pic2_button
          机器人:layout_width =WRAP_CONTENT
          机器人:layout_height =WRAP_CONTENT
          机器人:文本=你好,我是一个按钮/>
< / LinearLayout中>
 

viewer.xml:

 < XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT>
  < ImageView的机器人:ID =@ + ID / ImageView的
             机器人:layout_width =FILL_PARENT
             机器人:layout_height =FILL_PARENT
             机器人:SRC =@可绘制/ pingouin
             机器人:scaleType =矩阵>
  < / ImageView的>
< /的FrameLayout>
 

通过pic1.png,pic2.png和pingouin.png在RES /绘制...

Android newcomer here. I'm trying to setup an activity to display a selected image. The foundation of this code is from the Hello, Android 3rd edition (Pragmatic Programmer) that I have attempted to modify.

Basically, I want to be able to click a button in one activity, and have that start another activity that changes the layout to display the image associated with that button. I have several buttons, and would like each button to cause a different image to be displayed.

files (simplified code by removing pic3-...):

main.xml : layout for two buttons (pic1 and pic2)

Main.java : contains onclicklisteners for buttons - intents w/ extras (filename for image)

Viewer.java : default created by eclipse, purpose is to set new layout setContentView(R.layout.viewer);

viewer.xml : layout for ImageView

I have gotten strings stored in strings.xml to work (I create a string in eclipse with a value of @drawable/pic1 and give it a name of imagename so I can call @string/imagename for the src of ImageView in viewer.xml).

However, I have learned from searching and reading on this forum that I cannot change strings.xml values from within an activity (my original idea was to have a couple lines of code in Viewer.java that would change the imagename string to whatever was passed by the Intent extras.

I found this post (http://stackoverflow.com/questions/3523384/android-pass-string-from-activity-to-layout) where someone was trying to do a similar thing with a TextView, but I've tried that route and I keep getting syntax errors on those lines.

I'm really stuck. Any ideas? Thanks!

main.java

public class Main extends Activity implements OnClickListener {

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);


// click listeners
View pic1Button = findViewById(R.id.pic1_button);
pic1Button.setOnClickListener(this);
View pic2Button = findViewById(R.id.pic2_button);
pic2Button.setOnClickListener(this);

}

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

   case R.id.pic1_button:
      Intent l = new Intent(this, Viewer.class);
      l.putExtra("imagefilename", "pic1filename");
      startActivity(l);
      break;

   case R.id.pic2_button:
      Intent i = new Intent(this, Viewer.class);
      i.putExtra("imagefilename", "pic2filename");
      startActivity(i);
      break;

}
}
}

viewer.xml

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent">
   <ImageView android:id="@+id/imageView"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:src='@string/imagename'
      android:scaleType="matrix">
   </ImageView>

</FrameLayout>

解决方案

Your question is not very clear but from my understanding you could achieve that using the following code.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.stackoverflow.biowi"
      android:versionCode="1"
      android:versionName="1.0">
     <application android:label="@string/app_name" android:icon="@drawable/icon">
        <activity android:name="Main"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="Viewer" />
    </application>
</manifest>

Main.java:

package com.stackoverflow.biowi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class Main extends Activity implements OnClickListener
{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // click listeners
        View pic1Button = findViewById(R.id.pic1_button);
        pic1Button.setOnClickListener(this);
        View pic2Button = findViewById(R.id.pic2_button);
        pic2Button.setOnClickListener(this);
    }

    public void onClick(View v) {

        Intent i = new Intent(this, Viewer.class);

        switch (v.getId()) {
        case R.id.pic1_button:
            i.putExtra("imagefilename", "pic1filename");
            break;
        case R.id.pic2_button:
            i.putExtra("imagefilename", "pic2filename");
            break;
         }
        startActivity(i);
    }
}

Viewer.java:

package com.stackoverflow.biowi;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.widget.ImageView;

public class Viewer extends Activity
{

    /** To be documented. */
    private Bitmap mImage1;
    /** To be documented. */
    private Bitmap mImage2;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewer);

        Bundle bundle = getIntent().getExtras();
        String imageName = bundle.getString("imagefilename");


        // click listeners
        ImageView imageView = (ImageView)findViewById(R.id.imageView);

        mImage1 = BitmapFactory.decodeResource(getResources(), R.drawable.pic1);
        mImage2 = BitmapFactory.decodeResource(getResources(), R.drawable.pic2);

        if(imageName.matches("pic1filename")) {
            imageView.setImageBitmap(mImage1);
        } else if (imageName.matches("pic2filename")) {
            imageView.setImageBitmap(mImage2);
        }
    }

}

main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              >
  <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Hello World, Main"
      />
  <Button android:id="@+id/pic1_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Hello, I am a Button" />
  <Button android:id="@+id/pic2_button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Hello, I am a Button" />
</LinearLayout>

viewer.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <ImageView android:id="@+id/imageView"
             android:layout_width="fill_parent"
             android:layout_height="fill_parent"
             android:src='@drawable/pingouin'
             android:scaleType="matrix">
  </ImageView>
</FrameLayout>

With pic1.png, pic2.png and pingouin.png in res/drawable…

这篇关于Android的 - 从意向额外的ImageView的传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆