Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ //創建一個佈局管理器控制項 L
Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new Button.OnClickListener(){ public void onClick(View view){ //創建一個佈局管理器控制項 LinearLayout linearLayout=new LinearLayout(MainActivity.this); setContentView(linearLayout); //設置orientation為horizontal,1表示為vertical linearLayout.setOrientation(0); TextView tvSecond=new TextView(MainActivity.this); tvSecond.setText("這是Android奉獻給你的第二個頁面"); Button btNext2=new Button(MainActivity.this); btNext2.setText("下一步"); btNext2.setId(2); LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); linearLayout.addView(tvSecond,lp1); linearLayout.addView(btNext2,lp1); btNext2.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ //這次只放置一個TextView不需要佈局管理器 TextView tv3=new TextView(MainActivity.this); tv3.setText("恭喜你已來到最後一個頁面!"); //直接讓頁面顯示一個TextView setContentView(tv3); } }); } });
類似 Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new ButtonCreateClickListener()); } private class ButtonCreateClickListener implements View.OnClickListener { public void onClick(View view) { //創建一個佈局管理器控制項 LinearLayout linearLayout = new LinearLayout(MainActivity.this); setContentView(linearLayout); //設置orientation為horizontal,1表示為vertical linearLayout.setOrientation(0); TextView tvSecond = new TextView(MainActivity.this); tvSecond.setText("這是Android奉獻給你的第二個頁面"); Button btNext2 = new Button(MainActivity.this); btNext2.setText("下一步"); btNext2.setId(2); LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); linearLayout.addView(tvSecond, lp1); linearLayout.addView(btNext2, lp1); btNext2.setOnClickListener(new btNext2ClickListener()); } } private class btNext2ClickListener implements View.OnClickListener { public void onClick(View v) { //這次只放置一個TextView不需要佈局管理器 TextView tv3 = new TextView(MainActivity.this); tv3.setText("恭喜你已來到最後一個頁面!"); //直接讓頁面顯示一個TextView setContentView(tv3); } }
創建按鈕
Button btNext=(Button)findViewById(R.id.next); btNext.setOnClickListener(new ButtonCreateClickListener()); } private class ButtonCreateClickListener implements View.OnClickListener { public void onClick(View view) { //創建一個button按鈕 Button btn1 = new Button( MainActivity.this); btn1.setText("this is a button"); btn1.setId(1); TableLayout pTableLayout=(TableLayout)findViewById(R.id.table2); pTableLayout.addView(btn1); } }
可以是
private class ButtonCreateClickListener implements View.OnClickListener { public void onClick(View view) { //創建一個button按鈕 Button btn1 = new Button( MainActivity.this); btn1.setText("this is a button"); btn1.setId(1); TableRow pTableRow=(TableRow)findViewById(R.id.TableRow2); pTableRow.addView(btn1); } }