【问题标题】:How setcontentview in a new activity depending on the button clicked如何根据单击的按钮在新活动中设置内容视图
【发布时间】:2015-02-18 05:55:09
【问题描述】:

我一直在搜索论坛,但似乎找不到我要找的东西... 我有一个带有大约 40 个按钮的 xml。 每当单击特定按钮时,我都想打开一个新活动并根据单击的按钮设置内容视图。 打开新活动没问题,但我不知道如何将内容视图设置为正确的图像...

我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_regular"
        android:layout_marginRight="@dimen/margin_regular"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/chromatic_scale_bflat" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_bes1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/bes"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_b1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/b"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_c1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/c"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_cis1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/cis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_d1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/d"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:id="@+id/btn_dis1"
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/dis"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/e"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/f"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/fis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/g"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/gis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/a"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/chromatic_scale_bflat" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/bes"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/b"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/c"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/cis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/d"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/dis"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/e"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/f"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/fis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/g"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/gis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/a"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp" >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/chromatic_scale_bflat" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="3dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/bes"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/b"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/c"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/cis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/d"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/dis"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:orientation="horizontal" >

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/e"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/f"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/fis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/g"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/gis"
            android:textSize="@dimen/button_textsize" />

        <Button
            android:layout_width="54dp"
            android:layout_height="40dp"
            android:text="@string/a"
            android:textSize="@dimen/button_textsize" />
    </LinearLayout>

</LinearLayout>

我的 Java 文件:

package com.example.saxofoonnieuw;

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 FingeringChart extends Activity implements OnClickListener {

    Button bes1, b1, c1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fingering_chart);

        bes1 = (Button) findViewById(R.id.btn_bes1);
        bes1.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_bes1:
            Intent a = new Intent(this, PictureFingeringChart.class);
            startActivity(a);

            break;

        default:
            break;
        }
    }

}

【问题讨论】:

  • 通过intent.putExtra在bundle中传递图片名称或类似参数,并在另一个activity中获取该bundle
  • 没错。确实通过意图将一些独特的东西传递给另一个活动。
  • 你想在下一个活动的布局中设置图片吗?
  • 您可以在 Intent 中将图像资源作为额外内容传入,而不是设置内容视图。
  • 显示PictureFingeringChart.class的代码

标签: java android android-activity setcontentview android-bundle


【解决方案1】:

您应该按原样在 Intent 中添加额外信息:

在您的代码中,将 image_name 字符串变量传递给您的意图:

@Override
public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_bes1:
        Intent a = new Intent(this, PictureFingeringChart.class);
        String strName = "image_2"; // this variable changes for each button
        a.putExtra("image_name", strName);
        startActivity(a);

        break;

    default:
        break;
    }
}

现在在您的目标活动中获取变量名称:

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.fingering_chart);

    Intent intent = getIntent();
    String image_name = intent.getStringExtra("image_name");

    // now you have the image_name and you can go on... 

    // 1st get the reference to your ImageView :
    ImageView img = (ImageView) findViewById(R.id.image);
    // 2nd assume you pass as string param the name of the drawable, in this example, regarding previous code, we would like to load image_2.png, so we retrieve the corresponding resource ID like this :
    int resourceID = getResources().getIdentifier(image_name , "drawable", getPackageName());
    // 3rd then we assign it to our ImageView :
    img.setImageResource(resourceID);
    // 4th done!
}

【讨论】:

  • 这个解决方案对我来说是最熟悉的,但我仍然不知道如何将 imageview 设置为“drawable”文件夹中的图像。换句话说:我在你写的地方编码什么“//现在你有了image_name,你可以继续......”。
  • 太棒了!这可以完成工作。非常感谢!!
  • 很高兴它能帮到你!
【解决方案2】:

修改你的通话活动

活动1

 //initial part
     @Override
    public void onClick(View v) {
     Intent a = new Intent(this, PictureFingeringChart.class);
    switch (v.getId()) {
    case R.id.btn_bes1:

        a.putExtra("outputtodisplay",R.layout.viewToDisplay);


        break;

    default:
        break;
    }
    startActivity(a);
}

在你调用的活动中

//活动2

protected void onCreate(){
        super.onCreate()
setContentView(a.getIntExtra("outputtodisplay",R.layout.default_layout),

}

【讨论】:

    【解决方案3】:

    您可以将捆绑包传递给您的活动

    bundle.putByte("info", 1);
    startActivity(new Intent(this, PictureFingeringChart.class).putExtras(bundle));
    

    在 PictureFingeringChart.class 的 onCreate 方法中

    Bundle bundle = this.getIntent().getExtras();
            byte info = bundle.getByte("info");
    

    【讨论】:

      【解决方案4】:

      修改这个:

      @Override
      public void onClick(View v) {
          switch (v.getId()) {
          case R.id.btn_bes1:
              Intent a = new Intent(this, PictureFingeringChart.class);
              int img_ID = R.id.your_image;
              a.putExtra("EXTRA_ID", img_ID);
              startActivity(a);
              break;
      
          default:
              break;
          }
      }
      

      在 PictureFingeringChart.class 中:

       @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null) {
         int id = extras.get("EXTRA_ID");
         if (id!= null) {
              // setContentView or set your image here
         }        
      }
      

      而且由于您每次都调用不同的活动,因此您可以为各个活动设置不同的布局文件。

      【讨论】:

      • 如果它解决了您的问题..请检查此答案是否正确..谢谢,祝您有美好的一天:)
      【解决方案5】:

      试试这个...

      FingeringChart.java

      public static final IMAGE="package-name image id";
      public class FingeringChart extends Activity implements OnClickListener {
      
      Button bes1, b1, c1;
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.fingering_chart);
      
          bes1 = (Button) findViewById(R.id.btn_bes1);
          bes1.setOnClickListener(this);
      }
      
      @Override
      public void onClick(View v) {
          switch (v.getId()) {
          case R.id.btn_bes1:
              Intent a = new Intent(this, PictureFingeringChart.class);
              intent.putExtra(IMAGE,R.drawable.image1);
              startActivity(a);
      
              break;
      
          default:
              break;
          }
      }
      
      }
      

      PictureFingeringChart.java

      public class Test extends Activity{
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          int image = getIntent().getIntExtra(FingeringChart.IMAGE,0);
          ImageView imageView = new ImageView(this);
          imageView.setImageDrawable(getResources().getDrawable(image));
          setContentView(imageView);
      }
      }
      

      希望对你有帮助...

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 1970-01-01
        • 1970-01-01
        • 2013-04-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多