gpt4 book ai didi

android - 根据第二个 Activity android中的选择显示值

转载 作者:行者123 更新时间:2023-11-30 01:50:58 24 4
gpt4 key购买 nike

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

Activity 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);
}
});
}

Activity 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);
}
}

XML文件:

<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 来打开 Activity 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);
}
});

然后在您的 Activity2 onCreate() 中:

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

Intent intent = getIntent();
String key = intent.getStringExtra(YOUR_KEY);
if(key.equals("animalIntent")) {
btn1.setText("cat")
//same for other buttons
} else {
btn1.setText("red")
//same for other buttons
}
}

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

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

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

关于android - 根据第二个 Activity android中的选择显示值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33056262/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com