我正在尝试使用相同的活动文件和XML文件显示不同的值。我能够显示但不知道如何使用数组或其他东西动态显示值。抱歉英文不好请看图片
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);
}
}
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>
工作结果:
答案 0 :(得分:1)
当你按下颜色或动物按钮时,你应该使用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);
}
});
然后在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
}
}
如果您在实施上述示例时遇到问题,请查看developer.android.com上的this培训
修改强> 您还需要检查按钮上的按键onClick action:
public void btn1(View view) {
if(key.equals("animalIntent")) {
image.setBackgroundResource(R.drawable.black);
} else {
image.setBackgroundResource(R.drawable.white);
}
}
答案 1 :(得分:0)
现在一切正常。我也可以显示不同的图像。
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);
}
}