1

EditTextを動的に追加し、Androidで値を取得することができました。ここからプログラムコードを取得しました(以下のサンプル)

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

4

2 に答える 2

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;
    }
}

そしてここにoutpurprintscreenhttp ://a7.sphotos.ak.fbcdn.net/hphotos-ak-ash3/s720x720/599154_3041917178665_921465340_n.jpg

于 2012-06-05T11:18:21.590 に答える
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);
}
于 2012-06-04T17:06:28.357 に答える