简体   繁体   中英

edittext align left

I'm constructing an a sudokusolver-app for android phones.

The user is initially presented with a startpage where he/she is asked to type in the board dimension (4*4, 6*6 etc). Afterwards a new activity is started and a board with the chosen dimensions is drawn. This board consists of squares in the shape of EditText areas. I'm struggling with aligning the text on the left side of the EditText area. The text typed in the EditText area aligns slightly to the left but I want the text to be positioned in the corner. How do I do this? Im only using javacode, not 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{ }

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

android:gravity = "left";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM