20170117 RadioButton "多選一的單選鈕"功能練習(購票系統)
Android 練習的成果,RadioButton練習!
以下是程式碼
//20170117 劉彥廷
//RadioButton "多選一的單選鈕"功能練習(購票系統)
MainActivity.java
package com.example.user.myapplication26;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show(View v){
TextView txv = (TextView)findViewById(R.id.txv);
RadioGroup ticketType = (RadioGroup) findViewById(R.id.ticketType);
switch (ticketType.getCheckedRadioButtonId()){
case R.id.adult:
txv.setText("半票");
break;
case R.id.child:
txv.setText("全票");
break;
case R.id.senior:
txv.setText("敬老票");
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.user.myapplication26.MainActivity">
<Button
android:text="@string/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginTop="178dp"
android:onClick="show"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginTop="30dp"
android:id="@+id/txv"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:id="@+id/ticketType">
<RadioButton
android:text="@string/RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:id="@+id/adult" />
<RadioButton
android:text="@string/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/adult"
android:layout_alignRight="@+id/adult"
android:layout_alignEnd="@+id/adult"
android:id="@+id/child"
android:layout_weight="1" />
<RadioButton
android:text="@string/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/senior"
android:layout_below="@+id/child"
android:layout_alignLeft="@+id/child"
android:layout_alignStart="@+id/child"
android:layout_weight="1" />
</RadioGroup>
</RelativeLayout>
以下是程式碼
//20170117 劉彥廷
//RadioButton "多選一的單選鈕"功能練習(購票系統)
MainActivity.java
package com.example.user.myapplication26;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void show(View v){
TextView txv = (TextView)findViewById(R.id.txv);
RadioGroup ticketType = (RadioGroup) findViewById(R.id.ticketType);
switch (ticketType.getCheckedRadioButtonId()){
case R.id.adult:
txv.setText("半票");
break;
case R.id.child:
txv.setText("全票");
break;
case R.id.senior:
txv.setText("敬老票");
break;
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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="com.example.user.myapplication26.MainActivity">
<Button
android:text="@string/Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_marginTop="178dp"
android:onClick="show"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:layout_marginTop="30dp"
android:id="@+id/txv"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp"
android:id="@+id/ticketType">
<RadioButton
android:text="@string/RadioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:id="@+id/adult" />
<RadioButton
android:text="@string/RadioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/adult"
android:layout_alignRight="@+id/adult"
android:layout_alignEnd="@+id/adult"
android:id="@+id/child"
android:layout_weight="1" />
<RadioButton
android:text="@string/RadioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/senior"
android:layout_below="@+id/child"
android:layout_alignLeft="@+id/child"
android:layout_alignStart="@+id/child"
android:layout_weight="1" />
</RadioGroup>
</RelativeLayout>
strings.xml
<resources>
<string name="app_name">購票系統(No.26)</string>
<string name="Button">確定</string>
<string name="RadioButton">半票 </string>
<string name="RadioButton1">全票 </string>
<string name="RadioButton2">敬老票 </string>
</resources>
Vliacoelust-bi Gary High https://wakelet.com/wake/8bc8ZF_KFtYoXnvkZnGYK
回覆刪除carsingwhisfi