@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 按钮就会消失。我能得到任何帮助吗?