【Java开源代码栏目提醒】:网学会员,鉴于大家对Java开源代码十分关注,论文会员在此为大家搜集整理了“Controls3.java”一文,供大家参考学习!
package org.chapter3;
import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.ArrayAdapter;
import android.widget.ToggleButton;
/**
* A gallery of basic controls: Button, EditText, RadioButton, Checkbox,
* Spinner. This example uses the light theme.
*/
public class Controls3 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.controls_1);
ScrollView sv = new ScrollView(this);
ViewGroup.LayoutParams svLp = new ScrollView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(svLp);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LinearLayout.LayoutParams llLp1 = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams llLp2 = new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
Button btn = new Button(this);
btn.setText(R.string.controls_1_save);
btn.setLayoutParams(llLp1);
EditText et = new EditText(this);
et.setLayoutParams(llLp2);
CheckBox cb1 = new CheckBox(this);
cb1.setLayoutParams(llLp1);
cb1.setPadding(cb1.getPaddingLeft(), 24, cb1.getPaddingRight(), 24);
cb1.setText(R.string.controls_1_checkbox_1);
CheckBox cb2 = new CheckBox(this);
cb2.setLayoutParams(llLp1);
cb2.setText(R.string.controls_1_checkbox_2);
LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT);
RadioGroup rg = new RadioGroup(this);
RadioButton radioButton1 = new RadioButton(this);
radioButton1.setText(R.string.controls_1_radiobutton_1);
RadioButton radioButton2 = new RadioButton(this);
radioButton2.setText(R.string.controls_1_radiobutton_2);
rg.addView(radioButton1, 0, layoutParams);
rg.addView(radioButton2, 1, layoutParams);
CheckBox cb3 = new CheckBox(this);
cb3.setButtonDrawable(android.R.drawable.btn_star);
cb3.setLayoutParams(llLp1);
cb3.setText(R.string.controls_1_star);
ToggleButton tb1 = new ToggleButton(this);
tb1.setLayoutParams(llLp1);
ToggleButton tb2 = new ToggleButton(this);
tb2.setLayoutParams(llLp1);
Spinner s1 = new Spinner(this);
s1.setLayoutParams(llLp2);
ll.addView(btn, 0);
ll.addView(et, 1);
ll.addView(cb1, 2);
ll.addView(cb2, 3);
ll.addView(rg, 4);
ll.addView(cb3, 5);
ll.addView(tb1, 6);
ll.addView(tb2, 7);
ll.addView(s1, 8);
setContentView(sv);
//Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, mStrings);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
}
private static final String[] mStrings = {
"Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune"
};
}