在我的程序中,我希望它在单击按钮后在屏幕上显示灯光。 这可以直接将它放在onCreate上,这样一旦应用程序启动灯亮起,但是当我在onClick中使用相同的方法时,它似乎不起作用。
public class LightFlashGame extends Activity {
public static final int LIGHT_DIAM = 15;
final Lights lightModel = new Lights();
LightFlashView lightView;
private static int light = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lightView = new LightFlashView(this, lightModel);
//makeLights(lightModel, lightView, Color.RED); !!!Works when method is called here
setContentView(R.layout.activity_light_flash_game);
((RelativeLayout) findViewById(R.id.root)).addView(lightView, 0);
((Button) findViewById(R.id.btnBack)).setOnClickListener(
new Button.OnClickListener() {
@Override public void onClick(View v) {
Intent backScreen = new Intent(LightFlashGame.this, LightFlashMain.class);
startActivity(backScreen);
} });
((Button) findViewById(R.id.btnStart)).setOnClickListener(
new Button.OnClickListener(){
@Override public void onClick(View v){
makeLights(lightModel, lightView, Color.RED); !!!But not here
}
});
}
/** Install a context menu. */
@Override public void onCreateContextMenu(
ContextMenu menu,
View v,
ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
}
/** Respond to a context menu selection. */
@Override public boolean onContextItemSelected(MenuItem item) {
return super.onContextItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.light_flash_main, menu);
return true;
}
void makeLights(Lights lights, LightFlashView light, int color){
lights.addLight(
LIGHT_DIAM + (75),
LIGHT_DIAM + (75),
color,
LIGHT_DIAM);
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="fill_parent"
android:layout_height="fill_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"
tools:context=".LightFlashGame" >
<TextView
android:id="@+id/txtRound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnBack"
android:text="Round : "
android:textStyle="bold"
/>
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LightFlash"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
/>
<Button
android:id="@+id/btnBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:text="Back"
/>
<TextView
android:id="@+id/txtScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btnBack"
android:layout_alignParentRight="true"
android:text="Score : "
android:textStyle="bold" />
</RelativeLayout>
答案 0 :(得分:1)
您的方法不公开,请将其公开,然后立即尝试访问此内容!
public void makeLights(Lights lights, LightFlashView light, int color){
lights.addLight(
LIGHT_DIAM + (75),
LIGHT_DIAM + (75),
color,
LIGHT_DIAM);
}
在您的代码中找不到任何错误,请尝试更改它:
((Button) findViewById(R.id.btnStart)).setOnClickListener(
new Button.OnClickListener(){
@Override public void onClick(View v){
lightView = new LightFlashView(this, lightModel);
makeLights(lightModel, lightView, Color.RED);
}
});
答案 1 :(得分:1)
将您对makeLights()
中的onClick()
的通话更改为以下内容:
@Override
public void onClick(View v)
{
LightFlashGame.this.makeLights(lightModel, lightView, Color.RED);
}