D
D
dmitriycor882014-04-23 14:53:20
Java
dmitriycor88, 2014-04-23 14:53:20

How to insert into an activity with a View button?

I have a class like this:

package com.example.vid;



import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.Rect;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity  {
  
  private int puzzle[];
  private PuzzleView puzzleView;
    
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    puzzleView = new PuzzleView(this);
    setContentView(puzzleView);
    puzzleView.requestFocus();
    
    }
  public class PuzzleView extends View {
    private static final String TAG = "Sudoku" ;
    private final MainActivity game;
    private float width; // ширина одного тайла
    private float height; // высота одного тайла
    private int selX; // координата x выделенной области
    private int selY; // координата y выделенной области
    private final Rect selRect = new Rect();
    
    public PuzzleView(Context context) {
    super(context);
    this.game = (MainActivity) context;
    setFocusable(true);
    setFocusableInTouchMode(true);
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    width = w / 100f;
    height = h / 32f;
    getRect(selX, selY, selRect);
    Log.d(TAG, "onSizeChanged: width " + width + ", height "
    + height);
    super.onSizeChanged(w, h, oldw, oldh);
    }
    
    private void getRect(int x, int y, Rect rect) {
    rect.set((int) (x * width), (int) (y * height), (int) (x
    * width + width), (int) (y * height + height));
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
    // Рисование фона...
    Paint background = new Paint();
    background.setColor(getResources().getColor(
    R.color.puzzle_background));
    canvas.drawRect(0, 0, getWidth(), getHeight(), background);
    // Рисование игровой доски...
    // Рисование чисел...
    // Рисование подсказок...
    // Рисование выделения...
    // Рисование доски...
    // Определение цветов для линий решетки
    Paint dark = new Paint();
    dark.setColor(getResources().getColor(R.color.puzzle_dark));
    Paint hilite = new Paint();
    hilite.setColor(getResources().getColor(R.color.puzzle_hilite));
    Paint light = new Paint();
    light.setColor(getResources().getColor(R.color.puzzle_light));
    // Рисование вспомогательных линий решетки
    for (int i = 0; i < 32; i++) {
    canvas.drawLine(0, i * height, getWidth(), i * height,
    light);
    canvas.drawLine(0, i * height + 1, getWidth(), i * height
    + 1, hilite);
    canvas.drawLine(i * width, 0, i * width, getHeight(),
    light);
    canvas.drawLine(i * width + 1, 0, i * width + 1,
        
        getHeight(), hilite);
        }
        // Рисование основных линий решетки
        for (int i = 0; i < 100; i++) {
        //if (i % 5 != 0)
        //continue;
        canvas.drawLine(0, i * height, getWidth(), i * height,
        dark);
        
        canvas.drawLine(0, i * height + 1, getWidth(), i * height
        + 1, hilite);
        canvas.drawLine(i * width, 0, i * width, getHeight(), dark);
        canvas.drawLine(i * width + 1, 0, i * width + 1,
        getHeight(), hilite);
        }
        
        Paint selected = new Paint();
        selected.setColor(getResources().getColor(
        R.color.color_4));
        getRect(2, 0, selRect);
        canvas.drawRect(selRect, selected);
    
        
        
    }
    
    
  }

}

inherits from View and draws a black rectangle on the grid, but how to add buttons at the bottom? It turns out that you need to somehow call View from the layout, or make buttons from the class and limit the View. Suggest an idea.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mintormo, 2014-04-23
@dmitriycor88

Very simple. You have a regular view, which means it can be declared in XML in the same way along with standard buttons. Take, for example, LinearLayout and place your view and two standard buttons in it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question