首先,我的目標是在用戶單擊其他活動中的顏色選擇時,在一個活動中設置imageview背景顏色。但我不知道如何使用意圖來做到這一點。也許我需要了解更多。使用意圖設置圖像顏色(另一個活動)
這是我在第一次活動
public class Games1 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.games1);
ImageView wire1 = (ImageView)findViewById(R.id.imageView1);
ImageView wire2 = (ImageView)findViewById(R.id.imageView2);
ImageView wire3 = (ImageView)findViewById(R.id.imageView3);
ImageView wire4 = (ImageView)findViewById(R.id.imageView4);
ImageView wire5 = (ImageView)findViewById(R.id.imageView5);
ImageView wire6 = (ImageView)findViewById(R.id.imageView6);
ImageView wire7 = (ImageView)findViewById(R.id.imageView7);
ImageView wire8 = (ImageView)findViewById(R.id.imageView8);
wire1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
wire2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
wire3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
wire4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
wire5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
wire6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
wire7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
wire8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
}
代碼這是我的第二個活動(用戶選擇顏色)
public class SelectColor extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.selectcolor);
ImageView blue = (ImageView)findViewById(R.id.blue);
ImageView orange = (ImageView)findViewById(R.id.orange);
ImageView green = (ImageView)findViewById(R.id.green);
ImageView brown = (ImageView)findViewById(R.id.brown);
ImageView white = (ImageView)findViewById(R.id.white);
blue.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
Intent myIntent = new Intent(SelectColor.this, Games1.class);
startActivity(myIntent);
}
});
orange.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
green.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
brown.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
white.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
blue.setOnClickListener(new ImageView.OnClickListener() {
@Override
public void onClick(View v) {
DJ.keepMusicOn();
}
});
}
碼和最後這是我第二次活動「的.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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.zaidi.frame2.MainActivity2"
android:background="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Choose a color"
android:id="@+id/textView9"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#000000"
android:textSize="30dp"/>
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/blue"
android:src="@drawable/blue"
android:layout_above="@+id/green"
android:layout_toStartOf="@+id/green"
android:clickable="true" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/brown"
android:src="@drawable/brown"
android:layout_alignParentBottom="true"
android:layout_toEndOf="@+id/green"
android:clickable="true" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/green"
android:src="@drawable/green"
android:layout_above="@+id/brown"
android:layout_centerHorizontal="true"
android:clickable="true" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/orange"
android:src="@drawable/orange"
android:layout_above="@+id/green"
android:layout_toEndOf="@+id/green"
android:clickable="true" />
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/white"
android:src="@drawable/white"
android:layout_alignTop="@+id/brown"
android:layout_toStartOf="@+id/green"
android:clickable="true" />