android - 将变量从 Intent Extra 传递到 ImageView

发布于 2024-12-06 14:41:57 字数 2213 浏览 1 评论 0原文

安卓新人来了。我正在尝试设置一个活动来显示选定的图像。该代码的基础来自我尝试修改的《Hello,Android 第三版(实用程序员)》。

基本上,我希望能够单击一个活动中的按钮,然后启动另一个活动来更改布局以显示与该按钮关联的图像。我有几个按钮,并且希望每个按钮都能显示不同的图像。

文件(通过删除 pic3-... 简化代码):

main.xml :两个按钮的布局(pic1 和 pic2)

Main.java :包含按钮的 onclicklisteners - 带附加功能的意图(图像的文件名) )

Viewer.java : 默认由 eclipse 创建,目的是设置新布局 setContentView(R.layout.viewer);

viewer.xml:ImageView 的布局

我已将存储在 strings.xml 中的字符串用于工作(我在 eclipse 中创建一个值为 @drawable/pic1 的字符串,并为其指定一个名称 imagename,以便我可以调用 @string/imagename 作为查看器.xml 中 ImageView 的 src)。

然而,我从这个论坛上的搜索和阅读中了解到,我无法在活动中更改 strings.xml 值(我最初的想法是在 Viewer.java 中添加几行代码,将 imagename 字符串更改为传递的任何内容)通过 Intent extras,

我发现这篇文章(http://stackoverflow.com/questions/3523384/android-pass-string-from-activity-to-layout)有人试图用 TextView 做类似的事情,但是我已经尝试了这条路线,但我一直在这些行上遇到语法错误,

我真的有什么想法吗? 谢谢!

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;

}
}
}

查看器.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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟若柳尘 2024-12-13 14:41:57

您的问题不是很清楚,但根据我的理解,您可以使用以下代码来实现这一目标。

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>

在res/drawable中带有pic1.png,pic2.png和pingouin.png...

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…

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文