30.01.2014, 12:46. Просмотров 3736. Ответов 28
как сделать так чтобы при нажатии на кнопку в первом активити изменялись данные во втором активити, но без перехода в него, а переход осуществлялся при нажатии второй кнопки. Вот полный код первого и второго активити:
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| package com.example.activity01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class MainActivity extends Activity implements OnClickListener {
ImageButton btnActTwo;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnActTwo = (ImageButton) findViewById(R.id.BtnAct2);
btnActTwo.setOnClickListener(this);
btnActTwo = (ImageButton) findViewById(R.id.BtnAct3);
btnActTwo.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(this, MainActivity2.class);
switch (v.getId()) {
case R.id.BtnAct2:
startActivity(intent);
break;
case R.id.BtnAct3:
intent.putExtra("animm", R.drawable.animationdemo2);
}
}
} |
|
Java |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
| package com.example.activity02;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.graphics.drawable.AnimationDrawable;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MainActivity2 extends Activity {
MediaPlayer Aud;
MediaPlayer Aud2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity2);
Aud = MediaPlayer.create(this, R.raw.alarm);
Aud.setLooping(true);
Aud.start();
final ImageView img=(ImageView)findViewById(R.id.image1);
Bundle extras = getIntent().getExtras();
if (extras != null) {
int value = extras.getInt("animm");
}
else
img.setBackgroundResource(R.drawable.animationdemo);
img.post(new Runnable() {
@Override
public void run() {
AnimationDrawable animation=(AnimationDrawable)img.getBackground();
animation.start();
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_DOWN) {
Aud2 = MediaPlayer.create(this, R.raw.trew1);
Aud2.setLooping(true);
Aud2.start();
}
if (event.getAction() == MotionEvent.ACTION_UP)
{
Aud2.reset();
}
return super.onTouchEvent(event);
}
@Override
public void onPause(){
Aud.stop();
finish();
super.onPause();
}
} |
|
Надо поменять animationdemo на animationdemo2. Что я тут сделал не так?