以编程方式单击按钮时添加EditText和Spinner

时间:2014-02-20 09:33:05

标签: android android-edittext spinner add

所以我有一个类,当按下按钮时动态添加文本字段和微调器。问题是添加的文本字段和按钮没有正确对齐。它在彼此的顶部,但我希望它放在彼此旁边。我将方向设置为“水平”,但它不起作用。此外,项目列表不会显示添加的微调器。有人能帮我吗?我是android开发的新手。谢谢。这是我的代码。

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Spinner;

public class AddField extends Activity {

private LinearLayout mLayout, mLayout2;
private EditText qty, price ;
private Button mButton;
private Spinner mspinner;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_field);

    final List<String> list=new ArrayList<String>();

    list.add("Male");
    list.add("Female");


 final String[] str={"Male","Female"};

    mspinner = (Spinner)  findViewById(R.id.itemSpin);
    ArrayAdapter<String> adp1=new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1,list);
adp1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mspinner.setAdapter(adp1);



    mLayout = (LinearLayout) findViewById(R.id.linearLayout);
    mLayout2 = (LinearLayout) findViewById(R.id.linearLayout);





    qty = (EditText)  findViewById(R.id.inputQty);

    price = (EditText)  findViewById(R.id.inputPrice);

    mButton = (Button) findViewById(R.id.button);
    mButton.setOnClickListener(onClick());





}

private OnClickListener onClick() {
return new OnClickListener() {

@Override
public void onClick(View v) {
     mLayout2.addView(createNewSpin(mspinner.getContext().toString()));
    mLayout2.addView(createNewQty(qty.getText().toString()));
    mLayout2.addView(createNewPrice(price.getText().toString()));

}
};
}

private Spinner createNewSpin(String text) {


final LayoutParams lparams = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

final  Spinner spinner = new Spinner(this);
spinner.setLayoutParams(lparams);

return spinner;
}

private EditText createNewQty(String text) {


    final LayoutParams lparams = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    final  EditText editText = new EditText(this);
    editText.setLayoutParams(lparams);
    editText.setHint("Quantity");
    return editText;
}


private EditText createNewPrice(String text) {
final LayoutParams paramss = new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,       RelativeLayout.LayoutParams.WRAP_CONTENT);

final  EditText editText2 = new EditText(this);
editText2.setLayoutParams(paramss);
editText2.setHint("Price");
return editText2;
}

1 个答案:

答案 0 :(得分:0)

你能否出示你的进口部分?我想你应该在实例化你的视图之前创建LinearLayout.LayoutParams个对象并将ViewGroup.LayoutParams.WRAP_CONTENT传递给构造函数。

<强> UPD。 删除import android.app.ActionBar.LayoutParams并使用LinearLayout.LayoutParams课程,因为您在LinearLayout上以编程方式添加了视图。在此之后尝试将布局方向设置为水平。 此外,当您使用createNewSpin方法创建微调器时,您应该将适配器设置为它。更好的方法是在活动类中创建一个私有字段mSpinnerAdapter,并在onCreate时将其实例化一次。然后,每次新的createnewSpin调用都会设置此适配器:spinner.setAdapter(mSpinnerAdapter);并且您将在您创建的每个微调器中获取列表