我无法添加多个按钮

时间:2013-09-19 03:05:13

标签: android android-layout

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle presses on the action bar items
    switch (item.getItemId()) {

        case R.id.Numeric:
                createdialog();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
@SuppressLint("NewApi")
private void createdialog() {
    // TODO Auto-generated method stub


    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this,android.R.style.Theme_Holo_Light_Dialog);
    LayoutInflater inflater = MainActivity.this.getLayoutInflater();
    final View textenter = inflater.inflate(R.layout.button, null);
    final EditText userinputon = (EditText) textenter.findViewById(R.id.on);
    final EditText userinputoff = (EditText) textenter.findViewById(R.id.off);
    final EditText userinputwidth = (EditText) textenter.findViewById(R.id.width);
    final EditText userinputleft = (EditText) textenter.findViewById(R.id.left);
    final EditText userinputtop = (EditText) textenter.findViewById(R.id.top);
     builder.setView(textenter);
    builder.setTitle("Create Button");

    builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dInterface, int whichButton)
        {

            Value = userinputon.getText().toString();
            Value1 = userinputoff.getText().toString();
            Width = Integer.parseInt(userinputwidth.getText().toString());
            Left = Integer.parseInt(userinputleft.getText().toString());
            Top = Integer.parseInt(userinputtop.getText().toString());

            option();  
        }
    });
    builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   // User cancelled the dialog
               }
           });
    // Set other dialog properties

    // Create the AlertDialog
    AlertDialog dialog = builder.create();
    builder.show();
}

private void option() {
// TODO Auto-generated method stub

    setContentView(R.layout.activity_main);
        LinearLayout layout1 = (LinearLayout)findViewById(R.id.main);

    ToggleButton  myButton = new ToggleButton(this);

    myButton.setLayoutParams(params);
    myButton.setTextOn(Value);
    myButton.setTextOff(Value1+i);
        myButton.setWidth(Width);
    myButton.setX(Left);
    myButton.setY(Top);
    myButton.setId(i);

    layout1.addView(myButton);
}

我正在创建对话框,我正在添加布局按钮。我可以添加一个按钮但是如果我尝试添加另一个按钮,那么prev按钮就会消失。我可以得到任何帮助吗?

2 个答案:

答案 0 :(得分:0)

按钮可能在彼此之上。要使它们可见,您可以设置LinearLayout的方向。

您可以执行以下操作:

LinearLayout layout1 = (LinearLayout)findViewById(R.id.main);
layout1.setOrientation(LinearLayout.VERTICAL);

ToggleButton myButton = new ToggleButton(this);
ToggleButton myButton2 = new ToggleButton(this);

layout1.addView(myButton);
layout1.addView(myButton2);

答案 1 :(得分:0)

尝试这样的东西重新定位onCreate

上的setContentView
private LinearLayout layout;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    layout = (LinearLayout) findViewById(R.id.main)
}

//edit your option method to something like this
private void option(){
    ToggleButton  myButton = new ToggleButton(this);

    myButton.setLayoutParams(params);
    myButton.setTextOn(Value);
    myButton.setTextOff(Value1+i);
    myButton.setWidth(Width);
    myButton.setX(Left);
    myButton.setY(Top);
    myButton.setId(i);

   layout.addView(myButton);
}