0

我正在为安卓手机构建一个数独解算器应用程序。

用户最初会看到一个起始页,要求他/她输入板尺寸(4*4、6*6 等)。之后开始一个新的活动并绘制具有所选尺寸的板。该板由区域形状的正方形组成EditText。我正在努力对齐该区域左侧的文本EditText。在该EditText区域中键入的文本略微向左对齐,但我希望文本位于角落。我该怎么做呢?我只使用javacode,而不是xml

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{ }
4

1 回答 1

1

设置 EditText 属性,在 Eclipse 中,使用 Property 选项卡,将 Gravity 设置为 EditText 的左侧。

android:gravity = "left";
于 2012-07-09T18:22:32.340 回答