遇见一个问题,困扰了我很久,
我用封装.get .set从MusicService.java获得的随机随x传递到了main.java,但是不知道如何赋值到mian.java的按键监听中去。
具体如下
同一个包里有两个不同类,main.java是判断button产生int值是否与MusicService内产生的随机数相同的,部分有疑问的代码如下main.java的内容:
import com.MusicService;
...
...
public class main extends Activity{
//传递MusicService的变量i到main的x;
private MusicService ms;
public MusicService getMusicService(){
return ms;
}
public void set Music Service (MusicService ms){
this.ms = ms;
}
public static void main(String[] args){
main x = new main();
x.getMusicService().getI();
}
//我用封装.get .set从MusicService.java获得的随机随x,但是不知道如何赋值到按键监听
....
....
//按钮监听产生变量名为a的int值private Button.OnClickListener answer = new Button.OnClickListener(){
public void onClick(View v){
int a =0;
switch(v.getId()){
case R.id.button01:
a = 1;
break;
case R.id.button02"
a = 2;
break;
.....
.....
//一直到R.id.button09就不重复写了。
}
if ( ! (a == x) ){
Toast.makeText(main.this, " 测试fail", Toast.LENGTH_SHORT).show();
}
}
--------------------------------------------------------------------------------
MusicService.java的内容:
....
....
....
public class MusicService extends Service {
//用求模取绝对值的算法产生随机数int 1-9的变量i
int i = (Math.abs(new Random().nextInt()))%9+1;
//对MusicService中的i进行封装
public int getI(){
return i;
}
public void setI(int i){
this.i=i;
}
....
....
main.java的内容:
import com.MusicService;
...
...
public class main extends Activity{
//传递MusicService的变量i到main的x;
private MusicService ms;
public MusicService getMusicService(){
return ms;
}
public void set Music Service (MusicService ms){
this.ms = ms;
}
public static void main(String[] args){
main x = new main();
x.getMusicService().getI();
}
//我用封装.get .set从MusicService.java获得的随机随x,但是不知道如何赋值到按键监听
....
....
//按钮监听产生变量名为a的int值private Button.OnClickListener answer = new Button.OnClickListener(){
public void onClick(View v){
int a =0;
switch(v.getId()){
case R.id.button01:
a = 1;
break;
case R.id.button02"
a = 2;
break;
.....
.....
//一直到R.id.button09就不重复写了。
}
if ( ! (a == x) ){
Toast.makeText(main.this, " 测试fail", Toast.LENGTH_SHORT).show();
}
}
--------------------------------------------------------------------------------
MusicService.java的内容:
....
....
....
public class MusicService extends Service {
//用求模取绝对值的算法产生随机数int 1-9的变量i
int i = (Math.abs(new Random().nextInt()))%9+1;
//对MusicService中的i进行封装
public int getI(){
return i;
}
public void setI(int i){
this.i=i;
}
....
....
....
}
我用封装.get .set从MusicService.java获得的随机随x传递到了main.java,但是不知道如何赋值到mian.java的按键监听中去。
具体如下
同一个包里有两个不同类,main.java是判断button产生int值是否与MusicService内产生的随机数相同的,部分有疑问的代码如下main.java的内容:
import com.MusicService;
...
...
public class main extends Activity{
//传递MusicService的变量i到main的x;
private MusicService ms;
public MusicService getMusicService(){
return ms;
}
public void set Music Service (MusicService ms){
this.ms = ms;
}
public static void main(String[] args){
main x = new main();
x.getMusicService().getI();
}
//我用封装.get .set从MusicService.java获得的随机随x,但是不知道如何赋值到按键监听
....
....
//按钮监听产生变量名为a的int值private Button.OnClickListener answer = new Button.OnClickListener(){
public void onClick(View v){
int a =0;
switch(v.getId()){
case R.id.button01:
a = 1;
break;
case R.id.button02"
a = 2;
break;
.....
.....
//一直到R.id.button09就不重复写了。
}
if ( ! (a == x) ){
Toast.makeText(main.this, " 测试fail", Toast.LENGTH_SHORT).show();
}
}
--------------------------------------------------------------------------------
MusicService.java的内容:
....
....
....
public class MusicService extends Service {
//用求模取绝对值的算法产生随机数int 1-9的变量i
int i = (Math.abs(new Random().nextInt()))%9+1;
//对MusicService中的i进行封装
public int getI(){
return i;
}
public void setI(int i){
this.i=i;
}
....
....
main.java的内容:
import com.MusicService;
...
...
public class main extends Activity{
//传递MusicService的变量i到main的x;
private MusicService ms;
public MusicService getMusicService(){
return ms;
}
public void set Music Service (MusicService ms){
this.ms = ms;
}
public static void main(String[] args){
main x = new main();
x.getMusicService().getI();
}
//我用封装.get .set从MusicService.java获得的随机随x,但是不知道如何赋值到按键监听
....
....
//按钮监听产生变量名为a的int值private Button.OnClickListener answer = new Button.OnClickListener(){
public void onClick(View v){
int a =0;
switch(v.getId()){
case R.id.button01:
a = 1;
break;
case R.id.button02"
a = 2;
break;
.....
.....
//一直到R.id.button09就不重复写了。
}
if ( ! (a == x) ){
Toast.makeText(main.this, " 测试fail", Toast.LENGTH_SHORT).show();
}
}
--------------------------------------------------------------------------------
MusicService.java的内容:
....
....
....
public class MusicService extends Service {
//用求模取绝对值的算法产生随机数int 1-9的变量i
int i = (Math.abs(new Random().nextInt()))%9+1;
//对MusicService中的i进行封装
public int getI(){
return i;
}
public void setI(int i){
this.i=i;
}
....
....
....
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货