android 创建动态微调器

4zcjmb1e  于 2023-02-20  发布在  Android
关注(0)|答案(2)|浏览(102)

我有一个编辑文本,我在其中输入了一个数字。我想根据编辑文本中输入的数字创建微调。例如,如果我在编辑文本中输入2,我有2微调,如果4 ==〉4微调。
任何人可以张贴任何例子,并请建议哪种技术是更好地定义在Java或XML微调数据?

w51jfk4q

w51jfk4q1#

在以下代码中使用...

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

    });
}

}

4szc88ey

4szc88ey2#

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

相关问题