1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface;
import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout;
public class AddButtonActivity extends Activity implements OnClickListener { LinearLayout layout; private Button btn1; private int CurrentButtonNumber = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); layout= new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); btn1 = new Button(this); btn1.setId(CurrentButtonNumber); CurrentButtonNumber++; btn1.setText("SoftCat Go Button"); btn1.setOnClickListener(this); layout.addView(btn1, 150, 50); setContentView(layout); } public void onClick(View v) { switch(v.getId()){ case 0: Button TmpBtn = new Button(v.getContext()); TmpBtn.setText("I am Button" + String.valueOf(CurrentButtonNumber) ); TmpBtn.setId(CurrentButtonNumber); TmpBtn.setOnClickListener(this); LinearLayout.LayoutParams param =new LinearLayout.LayoutParams(100,50); CurrentButtonNumber++; layout.addView(TmpBtn, param); break; default: alertbox("I be hit.....oh...","you hit me . I am "+ String.valueOf(v.getId())); break; } } protected void alertbox(String title, String mymessage) { new AlertDialog.Builder(this) .setMessage(mymessage) .setTitle(title) .setCancelable(true) .setNeutralButton(android.R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton){} }) .show(); } }
|
1 則留言:
請問一下 新增到第八個會超出頁面
要怎麼辦??
張貼留言