Androidフォン用の数独ソルバーアプリを構築しています。
ユーザーには最初に、ボードの寸法 (4*4、6*6 など) を入力するよう求められる開始ページが表示されます。その後、新しいアクティビティが開始され、選択した寸法のボードが描かれます。このボードは、エリアの形をした正方形で構成されていEditText
ます。領域の左側にテキストを配置するのに苦労していますEditText
。領域に入力されたテキストEditText
はわずかに左に配置されますが、テキストを隅に配置したいと考えています。どうすればいいですか?私はxmlではなくjavacodeのみを使用しています
import java.util.ArrayList;
import sudoku.androis.R;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Bundle;
import android.text.Layout;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
public class SudokuUI extends Activity{
int dim;
EditText[][] board;
ScrollView sv;
RelativeLayout ll;
int params;
int boxHeight;
int boxWidth;
int textSize;
Button neste;
Button første;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
dim = getIntent().getExtras().getInt("key");
TextView textview = new TextView(this);
textview.setText(dim + "*" + dim + " brett");
if(dim == 4) {
params = 100;
boxHeight = 2;
boxWidth = 2;
textSize = 20;
}
else if(dim == 6) {
params = 70;
boxHeight = 2;
boxWidth = 3;
textSize = 20;
}
else if (dim == 9) {
params = 50;
boxHeight = 3;
boxWidth = 3;
textSize = 12;
}
else if (dim == 12) {
params = 40;
boxHeight = 3;
boxWidth = 4;
textSize = 5;
}
else if(dim == 16){
params = 30;
boxHeight = 4;
boxWidth = 4;
textSize = 2;
}
sv = new ScrollView(this);
ll = new RelativeLayout(this);
ll.addView(textview);
ll.setBackgroundDrawable(getResources().getDrawable(R.drawable.background1));
//ll.addView(sv);
board = new EditText[dim][dim];
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
board[i][j] = createEditText();
//board[i][j].setTextSize(textSize);
board[i][j].setBackgroundDrawable(getResources().getDrawable(R.drawable.square));
board[i][j].setTextColor(Color.parseColor("#ff0000"));
LinearLayout.LayoutParams marginParams = new LinearLayout.LayoutParams(board[i][j].getLayoutParams());
int paramOne = 0;
int paramTwo = 0;
for(int b = 1; b <= boxHeight; b++) {
for(int c = 1; c <= boxWidth; c++) {
if(i == boxWidth*b){
paramOne = 6;
} else if(j == boxHeight*c) {
paramTwo = 6;
}
}
}
marginParams.setMargins((i)*params + paramOne, (j+1)*params + paramTwo,0,0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
board[i][j].setLayoutParams(layoutParams);
ll.addView(board[i][j]);
}
}
neste = createNesteButton();
neste.setText("Neste");
LinearLayout.LayoutParams buttonPar = new LinearLayout.LayoutParams(neste.getLayoutParams());
buttonPar.setMargins(220,550,0,0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(buttonPar);
neste.setLayoutParams(layoutParams);
ll.addView(neste);
første = createNesteButton();
første.setText("Start");
LinearLayout.LayoutParams buttonPar2 = new LinearLayout.LayoutParams(første.getLayoutParams());
buttonPar2.setMargins(0,550,0,0);
RelativeLayout.LayoutParams layoutParams2 = new RelativeLayout.LayoutParams(buttonPar2);
første.setLayoutParams(layoutParams2);
ll.addView(første);
første.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int[][] numbers = new int[dim][dim];
for(int i = 0; i < dim; i++) {
for(int j = 0; j < dim; j++) {
try {
numbers[i][j] = Integer.parseInt(board[i][j].getText().toString());
validNumber(Integer.parseInt(board[i][j].getText().toString()));
} catch (NumberFormatException ne) {
numbers[i][j] = 0;
} catch (TooHighNumberException te) {
numbers[i][j] = 0;
}
}
}
}
});
ll.addView(sv);
setContentView(ll);
}
public EditText createEditText(){
final LayoutParams lparams;
lparams = new LayoutParams(params,params);
final EditText edittext = new EditText(this);
edittext.setGravity(Gravity.FILL_HORIZONTAL);
edittext.setLayoutParams(lparams);
return edittext;
}
public Button createNesteButton() {
final LayoutParams lparams;
lparams = new LayoutParams(200, 80);
final Button b = new Button(this);
b.setLayoutParams(lparams);
return b;
}
public void validNumber(int nr) throws TooHighNumberException {
if(nr > dim || nr < 0) {
throw new TooHighNumberException();
}
}
}
class TooHighNumberException extends Exception{ }