package level0.exercise.layout;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.MultiAutoCompleteTextView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* 都是一些基本的布局控件的基本實現,依次是:
* 兩個單選按鈕組,每組3人單選按鈕:RadioGroup&&RadioButton
* 四個復選框:CheckBox
* 一個下拉列表:Spinner
* 一個自動提示和一個多重自動提示:AutoCompleteTextView&&MultiAutoCompleteTextView
* @author level0
*
*/
public class LayoutExercise extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rg1 = (RadioGroup) findViewById(R.id.group1);
RadioGroup rg2 = (RadioGroup) findViewById(R.id.group2);
// RadioButton rb1 = (RadioButton)findViewById(R.id.choice1);
// RadioButton rb2 = (RadioButton)findViewById(R.id.choice2);
// RadioButton rb3 = (RadioButton)findViewById(R.id.choice3);
// RadioButton rb4 = (RadioButton)findViewById(R.id.choice4);
// RadioButton rb5 = (RadioButton)findViewById(R.id.choice5);
// RadioButton rb6 = (RadioButton)findViewById(R.id.choice6);
final TextView tv1 = (TextView) findViewById(R.id.tv1);
final TextView tv2 = (TextView) findViewById(R.id.tv2);
final TextView tv3 = (TextView) findViewById(R.id.tv3);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == R.id.choice1) {
showToast("1");
tv1.setText("1");
} else if (checkedId == R.id.choice2) {
showToast("2");
tv1.setText("2");
} else if (checkedId == R.id.choice3) {
showToast("3");
tv1.setText("3");
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == R.id.choice4) {
showToast("1");
tv2.setText("1");
} else if (checkedId == R.id.choice5) {
showToast("2");
tv2.setText("2");
} else if (checkedId == R.id.choice6) {
showToast("3");
tv2.setText("3");
}
}
});
final CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
final CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);
final CheckBox cb3 = (CheckBox) findViewById(R.id.cb3);
final CheckBox cb4 = (CheckBox) findViewById(R.id.cb4);
cb1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb1.isChecked()){
showToast("1");
tv3.setText("1");
}
}
});
cb2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb2.isChecked()){
showToast("2");
tv3.setText("2");
}
}
});
cb3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb3.isChecked()){
showToast("3");
tv3.setText("3");
}
}
});
cb4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb4.isChecked()){
showToast("4");
tv3.setText("4");
}
}
});
final String[] choices = {"a", "abc", "abcd", "abcde", "abcdef"};
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, choices);
//這個語句用于設置風格,明顯會比不設置要好看,可以通過注釋這個語句來看效果
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView tv4 = (TextView)findViewById(R.id.tv4);
tv4.setText(choices[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, choices);
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv);
actv.setAdapter(adapter1);
actv.setThreshold(1);
MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv);
mactv.setAdapter(adapter1);
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
private void showToast(String s) {
Toast.makeText(LayoutExercise.this, s, Toast.LENGTH_SHORT).show();
}
}
main.xml:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.MultiAutoCompleteTextView;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
/**
* 都是一些基本的布局控件的基本實現,依次是:
* 兩個單選按鈕組,每組3人單選按鈕:RadioGroup&&RadioButton
* 四個復選框:CheckBox
* 一個下拉列表:Spinner
* 一個自動提示和一個多重自動提示:AutoCompleteTextView&&MultiAutoCompleteTextView
* @author level0
*
*/
public class LayoutExercise extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RadioGroup rg1 = (RadioGroup) findViewById(R.id.group1);
RadioGroup rg2 = (RadioGroup) findViewById(R.id.group2);
// RadioButton rb1 = (RadioButton)findViewById(R.id.choice1);
// RadioButton rb2 = (RadioButton)findViewById(R.id.choice2);
// RadioButton rb3 = (RadioButton)findViewById(R.id.choice3);
// RadioButton rb4 = (RadioButton)findViewById(R.id.choice4);
// RadioButton rb5 = (RadioButton)findViewById(R.id.choice5);
// RadioButton rb6 = (RadioButton)findViewById(R.id.choice6);
final TextView tv1 = (TextView) findViewById(R.id.tv1);
final TextView tv2 = (TextView) findViewById(R.id.tv2);
final TextView tv3 = (TextView) findViewById(R.id.tv3);
rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == R.id.choice1) {
showToast("1");
tv1.setText("1");
} else if (checkedId == R.id.choice2) {
showToast("2");
tv1.setText("2");
} else if (checkedId == R.id.choice3) {
showToast("3");
tv1.setText("3");
}
}
});
rg2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
if (checkedId == R.id.choice4) {
showToast("1");
tv2.setText("1");
} else if (checkedId == R.id.choice5) {
showToast("2");
tv2.setText("2");
} else if (checkedId == R.id.choice6) {
showToast("3");
tv2.setText("3");
}
}
});
final CheckBox cb1 = (CheckBox) findViewById(R.id.cb1);
final CheckBox cb2 = (CheckBox) findViewById(R.id.cb2);
final CheckBox cb3 = (CheckBox) findViewById(R.id.cb3);
final CheckBox cb4 = (CheckBox) findViewById(R.id.cb4);
cb1.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb1.isChecked()){
showToast("1");
tv3.setText("1");
}
}
});
cb2.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb2.isChecked()){
showToast("2");
tv3.setText("2");
}
}
});
cb3.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb3.isChecked()){
showToast("3");
tv3.setText("3");
}
}
});
cb4.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if (cb4.isChecked()){
showToast("4");
tv3.setText("4");
}
}
});
final String[] choices = {"a", "abc", "abcd", "abcde", "abcdef"};
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, choices);
//這個語句用于設置風格,明顯會比不設置要好看,可以通過注釋這個語句來看效果
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
TextView tv4 = (TextView)findViewById(R.id.tv4);
tv4.setText(choices[arg2]);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, choices);
AutoCompleteTextView actv = (AutoCompleteTextView) findViewById(R.id.actv);
actv.setAdapter(adapter1);
actv.setThreshold(1);
MultiAutoCompleteTextView mactv = (MultiAutoCompleteTextView) findViewById(R.id.mactv);
mactv.setAdapter(adapter1);
mactv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
}
private void showToast(String s) {
Toast.makeText(LayoutExercise.this, s, Toast.LENGTH_SHORT).show();
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioGroup1"
/>
<RadioGroup
android:id="@+id/group1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/choice1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice1"/>
<RadioButton
android:id="@+id/choice2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice2"/>
<RadioButton
android:id="@+id/choice3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice3"/>
</RadioGroup>
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioGroup2"
/>
<RadioGroup
android:id="@+id/group2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/choice4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice1"/>
<RadioButton
android:id="@+id/choice5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice2"/>
<RadioButton
android:id="@+id/choice6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice3"/>
</RadioGroup>
<TextView
android:id="@+id/tv3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CheckBoxs"/>
<CheckBox
android:id="@+id/cb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="checkBox1"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkBox2"/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkBox3"/>
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkBox4"/>
<TextView
android:id="@+id/tv4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Spinner"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<MultiAutoCompleteTextView
android:id="@+id/mactv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioGroup1"
/>
<RadioGroup
android:id="@+id/group1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/choice1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice1"/>
<RadioButton
android:id="@+id/choice2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice2"/>
<RadioButton
android:id="@+id/choice3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice3"/>
</RadioGroup>
<TextView
android:id="@+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="RadioGroup2"
/>
<RadioGroup
android:id="@+id/group2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/choice4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice1"/>
<RadioButton
android:id="@+id/choice5"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice2"/>
<RadioButton
android:id="@+id/choice6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="choice3"/>
</RadioGroup>
<TextView
android:id="@+id/tv3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CheckBoxs"/>
<CheckBox
android:id="@+id/cb1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="checkBox1"/>
<CheckBox
android:id="@+id/cb2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkBox2"/>
<CheckBox
android:id="@+id/cb3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkBox3"/>
<CheckBox
android:id="@+id/cb4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="checkBox4"/>
<TextView
android:id="@+id/tv4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Spinner"/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<MultiAutoCompleteTextView
android:id="@+id/mactv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>