2012-07-09 40 views
0

我正在爲android手機構建一個sudokusolver應用程序。edittext align left

用戶初始時會出現一個開始頁面,要求他/她輸入電路板尺寸(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{ } 
+0

可否請你提供你有什麼到目前爲止一些示例代碼,也許一些圖片,什麼是目前發生的事情,你想怎樣呢?這將大大提高我們對情況的理解,從而加速解答。 – Morfic 2012-07-09 18:22:04

+0

嘗試我給的解決方案,如果它不工作..然後請粘貼你的XML或代碼在這裏.. – 2012-07-09 18:26:40

回答

1

設置的EditText屬性,在Eclipse中使用屬性選項卡,重力設置到左的EditText的。

android:gravity = "left";