创建动态微调器

时间:2018-04-02 10:07:28

标签: android android-edittext spinner

我有一个编辑文本,我输入数字。我想根据在编辑文本中输入的数字创建微调器。例如,如果我在编辑文本中输入2我有2个微调器,如果4 ==> 4个旋转器。 任何人都可以发布任何示例,并建议哪种技术更好地定义Java或Xml中的微调数据。

2 个答案:

答案 0 :(得分:0)

LinearLayout main_layer= (LinearLayout) findViewById(id.main_layer);

for (int i = 0; i < 2; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

LinearLayout layout = new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);

layout.setOrientation(LinearLayout.VERTICAL);
layout.setLayoutParams(params);

// Creating Spinner
Spinner spinner = new Spinner(this);
    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_spinner_item,
               spinnerArray); //selected item will look like a spinner set from XML
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout
                                                         .simple_spinner_dropdown_item);
    spinner.setAdapter(spinnerArrayAdapter);

layout.addView(button1);
main_layer.addView(layout);
}

答案 1 :(得分:0)

使用下面的代码...

public class MainActivity extends AppCompatActivity {
private EditText editText;
private Button mbtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initView();
    //   toRun();
}

private void initView() {
    editText = findViewById(R.id.amEtNumber);
    mbtn = findViewById(R.id.amBtnSave);
    LinearLayout main_layer = (LinearLayout) findViewById(R.id.main);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    LinearLayout layout = new LinearLayout(getApplicationContext());
    LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setLayoutParams(params);
    mbtn.setOnClickListener(view -> {
        int times = Integer.parseInt(editText.getText().toString().trim());
        for (int i = 0; i < times; i++) {
            List<String> spinnerArray = new ArrayList<>();
            spinnerArray.add("Select Item");
            spinnerArray.add("Item1");
            Spinner spinner = new Spinner(this);
            ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>
                    (this, android.R.layout.simple_spinner_item,
                            spinnerArray); //selected item will look like a spinner set from XML
            spinnerArrayAdapter.setDropDownViewResource(android.R.layout
                    .simple_spinner_dropdown_item);
            spinner.setAdapter(spinnerArrayAdapter);

            layout.addView(spinner);
        }
        main_layer.addView(layout);

    });
}

}