package com.android.dinolayout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
final LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("動態產生checkbox範例");
ll.addView(tv);
final EditText et = new EditText(this);
et.setText("3");
ll.addView(et);
Button b = new Button(this);
b.setText("按下以自動產生checkbox");
ll.addView(b);
//String ddd = String.valueOf(ll.getChildCount());
//Toast.makeText(et.getContext(), ddd, Toast.LENGTH_LONG).show();
b.setOnClickListener(new OnClickListener() {
int lare = 0;
@Override
public void onClick(View v) {
//確認該view存在,才會執行.
//String ddd = String.valueOf(ll.getChildCount());
//String xxx = String.valueOf(ll.getChildAt(0).toString());
//Toast.makeText(v.getContext(), ddd, Toast.LENGTH_LONG).show();
if (ll.getChildCount() != 3) {
//String ccc = "刪除";
//Toast.makeText(v.getContext(), ccc, Toast.LENGTH_LONG).show();
ll.removeViews(3, lare);
}
int length = Integer.parseInt(et.getText().toString()) ;
for(int i = 1 ; i <= length ; i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("I'm dynamic!"+ i);
ll.addView(cb);
lare = i;
}
}
});
this.setContentView(sv);
}
}
沒有留言:
張貼留言