简体   繁体   English

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

[英]TextView is not programmatically added to my Layout

I am trying to add TextViews to LinearLayout programmatically. 我正在尝试以编程方式将TextViews添加到LinearLayout。 The amount of TextViews is based on UserInput which he can type in using AlertDialog Builder But the TextViews are not added to the Layout. TextView的数量基于UserInput,他可以使用AlertDialog Builder键入该内容,但不会将TextView添加到布局中。 I don't know why. 我不知道为什么 Here is my whole code. 这是我的整个代码。 What is wrong in my code? 我的代码有什么问题?

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();


}
}

I already tested it! 我已经测试过了! and function perfect! 和功能完善! :) And I added the setWeightSum to distribute the textView :)并且我添加了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();
        }


        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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