TextView不是以编程方式添加到我的布局中

时间:2018-11-20 15:29:36

标签: android layout

我正在尝试以编程方式将TextViews添加到LinearLayout。 TextViews的数量基于UserInput,他可以使用AlertDialog Builder输入 但是TextViews不会添加到布局中。我不知道为什么这是我的整个代码。我的代码有什么问题?

public class HandleTableClick extends AppCompatActivity {
public int personsnumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.handle_table_click);
    final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
    final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Type number of persons");
    final EditText input = new EditText(this);
    input.setInputType(InputType.TYPE_CLASS_NUMBER);
    input.setRawInputType(Configuration.KEYBOARD_12KEY);
    String persons = input.getText().toString();
    try {
        personsnumber = Integer.parseInt(persons);
    }
    catch (NumberFormatException nfe){

    }
    alert.setView(input);
    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int whichButton) {
            TextView[] pairs=new TextView[personsnumber];
            for(int l=0; l<personsnumber; l++)
            {
                pairs[l] = new TextView(HandleTableClick.this);
                pairs[l].setTextSize(15);
                pairs[l].setLayoutParams(lp);
                pairs[l].setId(l);
                pairs[l].setText((l + 1) + ": something");
                myLayout.addView(pairs[l]);
            }
        }
    });

    alert.show();


}
}

1 个答案:

答案 0 :(得分:0)

我已经测试过了!和功能完善! :)   并且我添加了setWeightSum来分发textView

public class MainActivity extends AppCompatActivity {

        TextView textView;
        Integer personsnumber;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            personsnumber=0;
            final LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLayout);
            final LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT,1);

            AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Type number of persons");
            final EditText input = new EditText(this);
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setRawInputType(Configuration.KEYBOARD_12KEY);
            alert.setView(input);
            alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String persons = input.getText().toString();
                    try {
                        personsnumber = Integer.parseInt(persons);
                    }
                    catch (NumberFormatException nfe){
                    }
                    myLayout.setWeightSum(personsnumber);
                    for(int l=0; l<personsnumber; l++)
                    {
                        textView= new TextView(MainActivity.this);
                        textView.setTextSize(15);
                        textView.setId(l);
                        textView.setText((l + 1) + ": something");
                        textView.setLayoutParams(lp);
                        myLayout.addView(textView);

                    }
                    dialog.dismiss();
                }
            });
            alert.show();
        }


        }