我正在进行Android实验,该实验应该随机生成彩色托盘。用户可以自定义托盘的单元格数,然后按下骰子按钮随机化每个托盘的颜色,生成视觉上令人愉悦的颜色托盘。
现在,我只是试图实现一个按钮,该按钮应该向palletView添加一个单元格(来自Cells.java类)。现在,我将cellCount数据字段初始化为5,并且作为测试,该按钮应该调用addCell()方法,在那里它将cellCount更改为6.我运行了模拟器,但按钮赢了'回应。没有崩溃也没有错误,它什么也没做。
这里有一些截图,上面有说明此应用程序的当前状态和假定功能的说明
Emulater Screenshot 1 - 5 Cells
Emulator Screenshot 2 - 6 Cells
Cells.java的代码。这个课程和它的功能都有效,我只是无法通过imageButton来控制它们
package com.dexfenik.palletdice;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.widget.ImageButton;
public class Cells extends View {
// INITIALIZE THE CELL COUNTER (DEFAULT IS 5 FOR TEST)
private int cellCount = 5;
// INITIALIZE THE VALUES FOR THE COLOR CELLS
private Paint paint = new Paint();
// DATA FIELDS FOR GETTING SCREEN DENSITY FOR FIXED CELL SIZES
private Display display = ((Activity) getContext()).getWindowManager().getDefaultDisplay();
private float screenWidth = display.getWidth();
private float screenHeight = display.getHeight();
private final float top = 0;
private final float bottom = screenHeight/2;
public Cells (Context context, AttributeSet attrs) {
super(context, attrs);
}
public void oneCell(Canvas canvas) {...} // DRAWS ONE PALLET CELL IN THE PALLETVIEW
public void twoCells(Canvas canvas) {...} // DRAWS TWO CELLS IN THE PALLETVIEW
public void threeCells(Canvas canvas) {...} // DRAWS THREE CELLS IN THE PALLETVIEW
public void fourCells(Canvas canvas) {...} // DRAWS FOUR CELLS IN THE PALLETVIEW
public void fiveCells(Canvas canvas) {...} // DRAWS FIVE CELLS IN THE PALLETVIEW
public void sixCells(Canvas canvas) {...} // DRAWS SIX CELLS IN THE PALLETVIEW
//###################################################
// #
// INCREMENT CELL COUNT WITH + BUTTON // #
public void addCell(View view) { // # <-- THE BUTTON IS SUPPOSED TO RUN THIS METHOD
// # (cellCount is 6 as a test)
cellCount = 6; // #
} // #
//###################################################
// DECREMENT CELL COUNT WITH - BUTTON
public void removeCell() {...}
//###################################################
// #
@Override // #
public void onDraw (Canvas canvas) { // #
// #
switch (cellCount){ // #
case 1: oneCell(canvas); // #
break; // #
case 2: twoCells(canvas); // #
break; // #
case 3: threeCells(canvas); // #
break; // #
case 4: fourCells(canvas); // #
break; // #
case 5: fiveCells(canvas); // #
break; // #
case 6: sixCells(canvas); // # <-- WHEN "cellCount" IS 6, THERE WILL BE SIX COLOR CELLS
} // # (This method works when the cellCount variable is changed,
} // # but I can't get the button to implement that)
} // #
//###################################################
MainActivity.java
package com.dexfenik.palletdice;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.content.Context;
public class MainActivity extends Activity {
private Cells cells;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
cells = new Cells(this, null);
setContentView(cells);
setContentView(R.layout.activity_main);
ImageButton addCellBtn = (ImageButton) findViewById(R.id.addCell);
//ImageButton removeCellBtn = (ImageButton) findViewById(R.id.subCell); // NOT YET IMPLEMENTED
//ImageButton diceBtn = (ImageButton) findViewById(R.id.randomBtn); // NOT YET IMPLEMENTED
//##############################################################
// #
// TRYING TO GET THE ADD BUTTON TO WORK // #
addCellBtn.setOnClickListener(new View.OnClickListener() { // #
@Override // #
public void onClick(View v) { // #
cells.addCell(v); // # <-- THIS IS WHERE I'M TRYING TO GET THE BUTTON TO WORK
} // #
}); // #
} // #
// #
//##############################################################
}
activity_my.xml,我在其中构建了imageButton。我会在这里有这个,以防我在布局中遗漏了任何东西。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dexfenik.palletdice.MainActivity">
<!-- LAYOUT FOR PALLET CELLS -->
<RelativeLayout
android:id="@+id/palletView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5">
<com.dexfenik.palletdice.Cells
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/view"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<!-- LAYOUT FOR CONTROLS -->
<RelativeLayout
android:id="@+id/controlView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#1f1f1f">
<!-- IMAGEBUTTON FOR ADDCELL -->
<ImageButton
android:id="@+id/addCell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/plus"
android:contentDescription="@string/add"
android:layout_gravity="right"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:background="@android:color/transparent"
android:onClick="addCell"/>
</RelativeLayout>
</LinearLayout>
如果我可以获得有关如何实现此按钮的帮助,它将回答我关于如何从单独视图控制java图形的许多问题。谢谢。
更新:结果我使用View作为addCell的参数,而不是它声明的变量。我仍然有同样的问题
答案 0 :(得分:0)
在函数调用中,在onClick内部,view是一个未初始化的参数。也许你打算写cell.addCell(v)?
答案 1 :(得分:0)
addCellBtn.setOnClickListener(new View.OnClickListener() { // #
@Override // #
public void onClick(View v) { // #
cells.addCell(v); // # <-- THIS IS WHERE I'M TRYING TO GET THE BUTTON TO WORK
} // #
});