在Android中创建EditText视图行

时间:2012-06-04 16:56:31

标签: android android-layout user-interface android-edittext

我设法动态添加EditText并在Android中检索值,我从here获得了程序代码(下面的示例)

public class Sample extends Activity {
    private List<EditText> editTextList = new ArrayList<EditText>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(VERTICAL);

        int count = 10;
        linearLayout.addView(tableLayout(count));
        linearLayout.addView(submitButton());
        setContentView(linearLayout);
    }

    private Button submitButton() {
        Button button = new Button(this);
        button.setHeight(WRAP_CONTENT);
        button.setText("Submit");
        button.setOnClickListener(submitListener);
        return button;
    }

    // Access the value of the EditText

    private View.OnClickListener submitListener = new View.OnClickListener() {
        public void onClick(View view) {
            StringBuilder stringBuilder = new StringBuilder();
            for (EditText editText : editTextList) {
                stringBuilder.append(editText.getText().toString());
            }
        }
    };

    // Using a TableLayout as it provides you with a neat ordering structure

    private TableLayout tableLayout(int count) {
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setStretchAllColumns(true);
        int noOfRows = count / 5;
        for (int i = 0; i < noOfRows; i++) {
            int rowId = 5 * i;
            tableLayout.addView(createOneFullRow(rowId));
        }
        int individualCells = count % 5;
        tableLayout.addView(createLeftOverCells(individualCells, count));
        return tableLayout;
    }

    private TableRow createLeftOverCells(int individualCells, int count) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(0, 10, 0, 0);
        int rowId = count - individualCells;
        for (int i = 1; i <= individualCells; i++) {
            tableRow.addView(editText(String.valueOf(rowId + i)));
        }
        return tableRow;
    }

    private TableRow createOneFullRow(int rowId) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(0, 10, 0, 0);
        for (int i = 1; i <= 5; i++) {
            tableRow.addView(editText(String.valueOf(rowId + i)));
        }
        return tableRow;
    }     

}

这是输出打印屏幕 http://a7.sphotos.ak.fbcdn.net/hphotos-ak-ash3/582244_3038340529251_1585166028_31994124_463408868_n.jpg

如何制作EditText视图,如下图所示? http://a6.sphotos.ak.fbcdn.net/hphotos-ak-ash4/s720x720/401912_3038335049114_780299491_n.jpg

2 个答案:

答案 0 :(得分:1)

最后我得到了我想要的东西。 。

这里是代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


import static android.view.ViewGroup.LayoutParams.FILL_PARENT;
import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
import static android.widget.LinearLayout.VERTICAL;

public class main extends Activity {
    private List<EditText> editTextList = new ArrayList<EditText>();
    private int count = 10;
    private int ix ;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ScrollView sv = new ScrollView(this);
        LinearLayout linearLayout = new LinearLayout(this);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(FILL_PARENT, WRAP_CONTENT);
        linearLayout.setLayoutParams(params);
        linearLayout.setOrientation(VERTICAL); 

        linearLayout.addView(tableLayout(count));
        linearLayout.addView(submitButton());
        sv.isHorizontalScrollBarEnabled();
        sv.addView(linearLayout);
        setContentView(sv);
    }

    private Button submitButton() {
        Button button = new Button(this);
        button.setHeight(WRAP_CONTENT);
        button.setText("Submit");
        button.setOnClickListener(submitListener);
        return button;
    }
     // Access the value of the EditText

    private View.OnClickListener submitListener = new View.OnClickListener() {
        public void onClick(View view) {
            StringBuilder stringBuilder = new StringBuilder();
            String str[] =  new String[count];
            int i=0;
            for ( EditText editText : editTextList) {               
                stringBuilder.append(editText.getText().toString());      
                str[i]=editText.getText().toString();

                Toast msg = Toast.makeText(main.this, str[i], Toast.LENGTH_LONG);
                msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2, msg
                        .getYOffset() / 2);
                msg.show();
                i++;
            }

        }
    }; 
    // Using a TableLayout as it provides you with a neat ordering structure
     private TableLayout tableLayout(int count) {
        TableLayout tableLayout = new TableLayout(this);
        tableLayout.setStretchAllColumns(true);
       // int noOfRows = count / 5;
        for ( ix = count; ix >=0; ix--) {
            int rowId = count * ix;
            tableLayout.addView(createOneFullRow(rowId));
        }
        int individualCells = count % 5;
        return tableLayout;
    }
    private TableRow createOneFullRow(int rowId) {
        TableRow tableRow = new TableRow(this);
        tableRow.setPadding(0, 10, 0, 0);
        for (int i =count; i >= ix; i--) {

            tableRow.addView(editText(String.valueOf(rowId + i)));
        }
        return tableRow;
    }


    private EditText editText(String hint) {
        EditText editText = new EditText(this);
        editText.setId(Integer.valueOf(hint));
        editText.setHint(hint);
        editTextList.add(editText);
        return editText;
    }
}

这里是outpur印刷品 http://a7.sphotos.ak.fbcdn.net/hphotos-ak-ash3/s720x720/599154_3041917178665_921465340_n.jpg

答案 1 :(得分:0)

LinearLayout main,clild;
main = new LinearLayout(....); // with vertical orientation
main.setOrientation(LinearLayout.VERTICAL);


for(int i=0;i<5;i++)
{
    child = new LinearLayout(...); // with Horizontal orientation
    child.setOrientation(LinearLayout.HORIZONTAL);
    for(int j=0;j<i;j++)
    {
       EditText ed = new EditText(...);
       child.addView(ed);
    }
    main.addView(child);
}