Android 根据第二个活动中的选择显示值

Android 根据第二个活动中的选择显示值,android,android-activity,Android,Android Activity,我试图使用相同的活动文件和XML文件显示不同的值。我能够显示,但不知道如何使用数组或其他东西动态显示基于选择的值。抱歉英语不好,请看图片 活动1: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.menu); enableButtonEvents(); } private void enableBu

我试图使用相同的活动文件和XML文件显示不同的值。我能够显示,但不知道如何使用数组或其他东西动态显示基于选择的值。抱歉英语不好,请看图片

活动1:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    enableButtonEvents();
}

private void enableButtonEvents() {
    Button btnColor = (Button) findViewById(R.id.btnColor);
    btnColor.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent colorIntent = new Intent(MenuActivity.this, MainActivity.class);
            startActivity(colorIntent);
        }
    });  

    Button btnAnimal = (Button) findViewById(R.id.btnAnimal);
    btnAnimal.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent animalIntent = new Intent(MainActivity.this, MainActivity.class);
            startActivity(animalIntent);
        }
    });
}
活动2:

public class MainActivity extends Activity {

    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);  
    }

    public void red(View view) throws InterruptedException {
        image.setBackgroundResource(R.drawable.red);
    } 

    public void black(View view) {
        image.setBackgroundResource(R.drawable.black);
    }

    public void white(View view) {
        image.setBackgroundResource(R.drawable.white);
    }

    public void yellow(View view) {
        image.setBackgroundResource(R.drawable.yellow);
    }

    public void purple(View view) {
        image.setBackgroundResource(R.drawable.purple);
    }

    public void green(View view) {
        image.setBackgroundResource(R.drawable.green);
    }

    public void blue(View view) {
        image.setBackgroundResource(R.drawable.blue);
    }

    public void brown(View view) {
        image.setBackgroundResource(R.drawable.brown);
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/grey"
    tools:context=".MainActivity" >

    <ImageView 
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="125sp"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="50sp" />

    <Button
        android:id="@+id/black"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="black"
        android:background="@color/black"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/image" />

    <Button
        android:id="@+id/white"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="white"
        android:background="@color/white"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/image"
        android:layout_toRightOf="@id/black" />

    <Button
        android:id="@+id/brown"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="brown"
        android:background="@color/brown"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/black" />

    <Button
        android:id="@+id/blue"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="blue"
        android:background="@color/blue"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/white"
        android:layout_toRightOf="@id/brown" />

    <Button
        android:id="@+id/red"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="red"
        android:background="@color/red"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/brown" />

    <Button
        android:id="@+id/green"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="green"
        android:background="@color/green"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/blue"
        android:layout_toRightOf="@id/red" />
    <Button
        android:id="@+id/yellow"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="yellow"
        android:background="@color/yellow"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/red" />

    <Button
        android:id="@+id/purple"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="purple"
        android:background="@color/purple"
        android:layout_toRightOf="@id/yellow"
        android:layout_below="@id/green" />

</RelativeLayout>
Xml文件:

public class MainActivity extends Activity {

    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);  
    }

    public void red(View view) throws InterruptedException {
        image.setBackgroundResource(R.drawable.red);
    } 

    public void black(View view) {
        image.setBackgroundResource(R.drawable.black);
    }

    public void white(View view) {
        image.setBackgroundResource(R.drawable.white);
    }

    public void yellow(View view) {
        image.setBackgroundResource(R.drawable.yellow);
    }

    public void purple(View view) {
        image.setBackgroundResource(R.drawable.purple);
    }

    public void green(View view) {
        image.setBackgroundResource(R.drawable.green);
    }

    public void blue(View view) {
        image.setBackgroundResource(R.drawable.blue);
    }

    public void brown(View view) {
        image.setBackgroundResource(R.drawable.brown);
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/grey"
    tools:context=".MainActivity" >

    <ImageView 
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="125sp"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="50sp" />

    <Button
        android:id="@+id/black"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="black"
        android:background="@color/black"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/image" />

    <Button
        android:id="@+id/white"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="white"
        android:background="@color/white"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/image"
        android:layout_toRightOf="@id/black" />

    <Button
        android:id="@+id/brown"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="brown"
        android:background="@color/brown"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/black" />

    <Button
        android:id="@+id/blue"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="blue"
        android:background="@color/blue"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/white"
        android:layout_toRightOf="@id/brown" />

    <Button
        android:id="@+id/red"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="red"
        android:background="@color/red"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/brown" />

    <Button
        android:id="@+id/green"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="green"
        android:background="@color/green"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/blue"
        android:layout_toRightOf="@id/red" />
    <Button
        android:id="@+id/yellow"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="yellow"
        android:background="@color/yellow"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/red" />

    <Button
        android:id="@+id/purple"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="purple"
        android:background="@color/purple"
        android:layout_toRightOf="@id/yellow"
        android:layout_below="@id/green" />

</RelativeLayout>

工作结果:

public class MainActivity extends Activity {

    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.image);  
    }

    public void red(View view) throws InterruptedException {
        image.setBackgroundResource(R.drawable.red);
    } 

    public void black(View view) {
        image.setBackgroundResource(R.drawable.black);
    }

    public void white(View view) {
        image.setBackgroundResource(R.drawable.white);
    }

    public void yellow(View view) {
        image.setBackgroundResource(R.drawable.yellow);
    }

    public void purple(View view) {
        image.setBackgroundResource(R.drawable.purple);
    }

    public void green(View view) {
        image.setBackgroundResource(R.drawable.green);
    }

    public void blue(View view) {
        image.setBackgroundResource(R.drawable.blue);
    }

    public void brown(View view) {
        image.setBackgroundResource(R.drawable.brown);
    }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@color/grey"
    tools:context=".MainActivity" >

    <ImageView 
        android:id="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="125sp"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="50sp" />

    <Button
        android:id="@+id/black"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="black"
        android:background="@color/black"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/image" />

    <Button
        android:id="@+id/white"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="white"
        android:background="@color/white"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/image"
        android:layout_toRightOf="@id/black" />

    <Button
        android:id="@+id/brown"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="brown"
        android:background="@color/brown"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/black" />

    <Button
        android:id="@+id/blue"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="blue"
        android:background="@color/blue"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/white"
        android:layout_toRightOf="@id/brown" />

    <Button
        android:id="@+id/red"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="red"
        android:background="@color/red"
        android:layout_marginBottom="50sp"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/brown" />

    <Button
        android:id="@+id/green"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="green"
        android:background="@color/green"
        android:layout_marginBottom="50sp"
        android:layout_below="@id/blue"
        android:layout_toRightOf="@id/red" />
    <Button
        android:id="@+id/yellow"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="yellow"
        android:background="@color/yellow"
        android:layout_marginRight="100sp"
        android:layout_marginLeft="50sp"
        android:layout_below="@id/red" />

    <Button
        android:id="@+id/purple"
        android:layout_width="200sp"
        android:layout_height="100sp"
        android:onClick="purple"
        android:background="@color/purple"
        android:layout_toRightOf="@id/yellow"
        android:layout_below="@id/green" />

</RelativeLayout>

当您按下颜色或动物按钮时,应使用带有附加功能的
Intent
打开活动2

例如:

Button btnColor = (Button) findViewById(R.id.btnColor);
    btnColor.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent colorIntent = new Intent(MenuActivity.this, Activity2.class);
            colorIntent.putExtra("YOUR_KEY", "colorIntent")
            startActivity(colorIntent);
        }
    });

Button btnColor = (Button) findViewById(R.id.btnColor);
    btnColor.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            Intent animalIntent = new Intent(MenuActivity.this, Activity2.class);
            animalIntent.putExtra("YOUR_KEY", "animalIntent")
            startActivity(animalIntent);
        }
    });
然后在您的
活动2
onCreate()

如果您在实现上述示例时遇到问题,请查看developer.android.com上的培训

编辑: 您还需要检查按钮onClick操作中的键:

public void btn1(View view) {
    if(key.equals("animalIntent")) {
        image.setBackgroundResource(R.drawable.black);
    } else {
        image.setBackgroundResource(R.drawable.white);
    }
}

现在一切正常。我也能展示不同的图像

    public class Main extends Activity {

    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.drag);
        Intent intent = getIntent();
        String key = intent.getStringExtra("YOUR_KEY");
        image = (ImageView) findViewById(R.id.image);

        Button btn1,btn2;
        btn1 = (Button) findViewById(R.id.btn1);
        btn2 = (Button) findViewById(R.id.btn2);

        if(key.equals("animalIntent") )
    {
        btn1.setText("cat");
        btn2.setText("dog");


    } else {
        btn1.setText("red");
        btn2.setText("blue");
    }
}

    public void btn1(View view) {
        Intent intent = getIntent();
        String key = intent.getStringExtra("YOUR_KEY");
        if(key.equals("animalIntent") )
        {
            image.setBackgroundResource(R.drawable.cat);
        } else {
            image.setBackgroundResource(R.drawable.red);
        }
    }

    public void btn2(View view) {
        Intent intent = getIntent();
        String key = intent.getStringExtra("YOUR_KEY");
        if(key.equals("animalIntent") )
        {
          image.setBackgroundResource(R.drawable.dog);


        } else {
          image.setBackgroundResource(R.drawable.blue);
        }
    }

到底是什么不适合你?如果出现错误,请指定我使用了不带引号的密钥,因为该密钥通常是在前面定义的(
String YOUR\u KEY=“yourKey”
),然后使用不带引号的密钥。但您可以在键的引号中使用“YOUR_KEY”显示什么值?再次尝试使用编辑过的答案,您可能使用
startActivity(colorIntent)两个时间编辑我的答案,查看链接了解更多详细指南这与“你的钥匙”有关吗?我应该在哪里定义这个键?刚刚在答案中编辑了另一个输入错误,请尝试使用它