我的老虎机仍在进行中。我试图从一个类到另一个类获得一个方法,但我无法弄明白。有人可以帮帮我吗?这是我想要从另一个类调用该方法的第一个代码:
GameMainActivity:
package com.ics136leeward.slotmachine;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class GameMainActivity extends Activity {
ViewFlipper slotOne, slotTwo, slotThree, spinStop;
Button spin, stop, bet;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_main);
this.initSpin();
this.initStop();
}
private void initSpin() { //initialize Spin Method
spin = (Button) findViewById (R.id.spinBtn);
slotOne = (ViewFlipper) findViewById (R.id.slot1);
slotTwo = (ViewFlipper) findViewById (R.id.slot2);
slotThree = (ViewFlipper) findViewById (R.id.slot3);
spinStop = (ViewFlipper) findViewById (R.id.spinstopbutton);
spin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slotOne.setFlipInterval(40);
slotOne.startFlipping(); //slot 1 spin
slotTwo.setFlipInterval(50);
slotTwo.startFlipping(); //slot 2 spin
slotThree.setFlipInterval(60);
slotThree.startFlipping(); //slot 3 spin
spinStop.showNext(); // shows the stop button
}
});
}
private void initStop() { //initialize Stop Method
stop = (Button) findViewById (R.id.stopBtn);
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
slotOne.stopFlipping(); //stops slot 1
slotTwo.stopFlipping(); //stops slot 2
slotThree.stopFlipping(); //stops slot 3
spinStop.showNext(); //shows the spin button again
if(slotOne == slotTwo || slotTwo == slotThree) {
}
}
});
}
}
这是第二个java类,我想把方法getBet1()和getBet5()调用到第一个活动:
赌注:
package com.ics136leeward.slotmachine;
import android.app.Activity;
import android.widget.TextView;
public class Bet extends Activity {
TextView userBet, bankRoll, event;
final int BETONE = 1, BETFIVE = 5;
int uBet = 100, bet;
public void getBet1() {
userBet = (TextView) findViewById (R.id.userBet);
bankRoll = (TextView) findViewById (R.id.bankroll);
uBet -= BETONE;
bet += BETONE;
userBet.setText("Your Bet: " + bet);
bankRoll.setText("" + uBet);
return;
}
public void getBet5() {
userBet = (TextView) findViewById (R.id.userBet);
bankRoll = (TextView) findViewById (R.id.bankroll);
uBet -= BETFIVE;
bet += BETFIVE;
userBet.setText("Your Bet: " + bet);
bankRoll.setText("" + uBet);
return;
}
}
答案 0 :(得分:1)
您需要创建一个Utility类而不是Activity类
所以改变
public class Bet extends Activity {
到
public class Bet // Normal java class
由于它不是Activity类,因此无需初始化视图
userBet = (TextView) findViewById (R.id.userBet); //remove them
// Initialize all yours views in Activity
现在在Activity类
中Bet bet = new Bet();
int value =bet.getBet1();
在getBet1()
中,您是否计算了返回值。
然后在Activity
中,您可以将值设置为TextView
textView.setText(String.valueOf(value));
还要检查raghav的回答@
答案 1 :(得分:0)
您可以定义这些方法static
,然后按其class name
调用
Bet.getBet1();
Bet.getBet5();
或者只是您可以创建类object
然后调用它
Bet b = new Bet();
b.getBet1()
答案 2 :(得分:0)
注意:由于您没有使用任何UI,因此无需扩展到Activity类。 (已经由Raghunandan提出)
在您的调用类的onCreate
方法中尝试此代码,即GameMainActivity
Bet bet= new Bet(); // where bet is object of Bet Class
bet.getBet1();
bet.getBet5();
但是,您可以在任何方法中创建Bet
类的对象,并访问类方法,前提是其访问修饰符必须为public
。