- 按下按鈕,顯示EditView輸入的內容和選到的CheckBox
- 顯示框出現,利用RadioGroup選擇Show出訊息或隱藏訊息
- 按下清除按鈕,EditView、CheckBox 回復初始狀態
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
private Button press; | |
private Button clear; | |
private TextView Name; | |
private TextView msg; | |
private ArrayList<CheckBox> cbList = new ArrayList<CheckBox>(); | |
private RadioGroup rGroup; | |
private RadioButton btnShow; | |
private RadioButton btnHide; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
findView(); | |
SetDefault(); | |
//msg內容 | |
press.setOnClickListener(new Button.OnClickListener(){ | |
@Override | |
public void onClick(View v){ | |
String r=""; | |
//第一種CheckBox批次跑迴圈寫法 | |
int ckID[]={R.id.checkBox1,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,R.id.checkBox6}; | |
CheckBox a =null; | |
for(int i=0 ;i<ckID.length;i++) | |
{ | |
if((a=(CheckBox)findViewById(ckID[i]))!=null) | |
{ | |
if(a.isChecked()) | |
{ | |
r+=a.getText()+","; | |
} | |
} | |
} | |
setTitle("MyApp1 Name:" + Name.getText()); | |
msg.setText("checked: " + r.substring(0,r.length()-1) ); | |
rGroup.setVisibility(View.VISIBLE); | |
} | |
}); | |
//控制 msg可見 | |
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { | |
@Override | |
public void onCheckedChanged(RadioGroup group, int checkedId) { | |
if (checkedId == btnHide.getId()) { | |
msg.setVisibility(View.GONE); | |
} else if (checkedId == btnShow.getId()) { | |
msg.setVisibility(View.VISIBLE); | |
} | |
} | |
}); | |
} | |
private void findView(){ | |
msg=(TextView)findViewById(R.id.textView4); | |
press = (Button)findViewById(R.id.button1); | |
clear=(Button)findViewById(R.id.btnclear); | |
Name=(TextView)findViewById(R.id.editText); | |
rGroup=(RadioGroup)findViewById(R.id.rGroup); | |
btnShow =(RadioButton)findViewById(R.id.radioButton1); | |
btnHide =(RadioButton)findViewById(R.id.radioButton2); | |
CheckBox ck1 =(CheckBox)findViewById(R.id.checkBox1); | |
CheckBox ck2 =(CheckBox)findViewById(R.id.checkBox2); | |
CheckBox ck3 =(CheckBox)findViewById(R.id.checkBox3); | |
CheckBox ck4 =(CheckBox)findViewById(R.id.checkBox4); | |
CheckBox ck5 =(CheckBox)findViewById(R.id.checkBox5); | |
CheckBox ck6 =(CheckBox)findViewById(R.id.checkBox6); | |
cbList.add(ck1); | |
cbList.add(ck2); | |
cbList.add(ck3); | |
cbList.add(ck4); | |
cbList.add(ck5); | |
cbList.add(ck6); | |
} | |
//清除 | |
private void SetDefault(){ | |
clear.setOnClickListener(new Button.OnClickListener(){ | |
@Override | |
public void onClick(View v) | |
{ | |
//第二種CheckBox批次跑迴圈寫法 | |
//批次取消CheckBox選項 | |
for (CheckBox c: cbList) { | |
c.setChecked(false); | |
} | |
msg.setText("Click Clear"); | |
Name.setText(""); | |
setTitle("MyApp1"); | |
msg.setVisibility(View.VISIBLE); | |
rGroup.setVisibility(View.GONE); | |
} | |
}); | |
} | |
//msg可見 | |
private void SetMsgVisiable(){ | |
} | |
public boolean onCreateOptionsMenu(Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected(MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:paddingBottom="@dimen/activity_vertical_margin" | |
android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
tools:context=".MainActivity"> | |
<TextView | |
android:id="@+id/txt1" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:autoText="false" | |
android:text="@string/hello_world" | |
/> | |
<EditText | |
android:id="@+id/editText" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:ems="10" | |
android:inputType="textPersonName" | |
android:layout_marginTop="29dp" | |
android:hint="請輸入姓名" | |
android:layout_below="@+id/txt1" | |
android:layout_centerHorizontal="true" /> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="帳號" | |
android:textAppearance="?android:attr/textAppearanceLarge" | |
android:layout_alignBaseline="@+id/editText" | |
android:layout_alignBottom="@+id/editText" | |
android:layout_alignParentStart="true" /> | |
<CheckBox | |
android:id="@+id/checkBox1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:checked="false" | |
android:text="寫程式" | |
android:textSize="@dimen/abc_text_size_small_material" | |
android:layout_below="@+id/editText" | |
android:layout_alignParentStart="true" | |
android:layout_marginTop="27dp" /> | |
<CheckBox | |
android:id="@+id/checkBox2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignTop="@+id/checkBox1" | |
android:layout_marginStart="23dp" | |
android:layout_toEndOf="@+id/checkBox1" | |
android:checked="false" | |
android:text="聽音樂" /> | |
<CheckBox | |
android:id="@+id/checkBox3" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignBottom="@+id/checkBox2" | |
android:layout_marginStart="30dp" | |
android:layout_toEndOf="@+id/checkBox2" | |
android:checked="false" | |
android:text="看書" /> | |
<Button | |
android:id="@+id/button1" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="顯示" | |
android:layout_centerVertical="true" | |
android:layout_alignParentStart="true" /> | |
<Button | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="清除" | |
android:id="@+id/btnclear" | |
android:layout_below="@+id/button1" | |
android:layout_alignParentStart="true" /> | |
<TextView | |
android:id="@+id/textView4" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:textColor="#405dff" | |
android:layout_below="@+id/rGroup" | |
android:layout_alignParentStart="true" | |
android:layout_alignParentBottom="true" /> | |
<RadioGroup | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@+id/btnclear" | |
android:layout_alignParentStart="true" | |
android:id="@+id/rGroup" | |
android:visibility="invisible"> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="show msg" | |
android:id="@+id/radioButton1" | |
android:checked="true" | |
/> | |
<RadioButton | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="Hide msg" | |
android:id="@+id/radioButton2" | |
android:layout_below="@+id/btnclear" | |
android:layout_alignParentStart="true" | |
android:checked="false" | |
/> | |
</RadioGroup> | |
<CheckBox | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="運動 " | |
android:id="@+id/checkBox4" | |
android:layout_below="@+id/checkBox1" | |
android:layout_alignParentStart="true" | |
android:checked="false" /> | |
<CheckBox | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="睡覺" | |
android:id="@+id/checkBox5" | |
android:checked="false" | |
android:layout_below="@+id/checkBox2" | |
android:layout_alignStart="@+id/checkBox2" /> | |
<CheckBox | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="看電影" | |
android:id="@+id/checkBox6" | |
android:checked="false" | |
android:layout_below="@+id/checkBox3" | |
android:layout_toEndOf="@+id/checkBox5" | |
android:layout_alignStart="@+id/checkBox3" /> | |
</RelativeLayout> |
沒有留言:
張貼留言