Android 自寫範例-下拉式選單(Spinner)+單選清單(RadioGroup+RadioButton)

Android 自寫範例-下拉式選單(Spinner)+單選清單(RadioGroup+RadioButton)

Android 自寫範例-下拉式選單(Spinner)+單選清單(RadioGroup+RadioButton)

 

xml

<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/textView1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”起始站” />

<TextView
android:id=”@+id/textView2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView1″
android:layout_below=”@+id/textView1″
android:layout_marginTop=”69dp”
android:text=”終點站” />

<Spinner
android:id=”@+id/spinner1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignTop=”@+id/textView1″
android:layout_marginLeft=”120dp”
android:layout_toRightOf=”@+id/textView1″ />

<Spinner
android:id=”@+id/spinner2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/spinner1″
android:layout_alignTop=”@+id/textView2″ />

<RadioGroup
android:id=”@+id/radioGroup1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentLeft=”true”
android:layout_below=”@+id/spinner2″ >

<RadioButton
android:id=”@+id/radio0″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:checked=”true”
android:text=”成人” />

<RadioButton
android:id=”@+id/radio1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”老人” />

<RadioButton
android:id=”@+id/radio2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”兒童” />
</RadioGroup>

<TextView
android:id=”@+id/textView3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/radioGroup1″
android:layout_below=”@+id/radioGroup1″
android:layout_marginTop=”36dp”
android:text=”張數” />

<EditText
android:id=”@+id/editText1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBaseline=”@+id/textView3″
android:layout_alignBottom=”@+id/textView3″
android:layout_alignRight=”@+id/spinner2″
android:ems=”10″ />

<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/textView3″
android:layout_below=”@+id/editText1″
android:layout_marginTop=”21dp”
android:text=”Button” />

<TextView
android:id=”@+id/textView4″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignRight=”@+id/radioGroup1″
android:layout_below=”@+id/button1″
android:layout_marginTop=”18dp”
android:text=”TextView” />

</RelativeLayout>

 

code

package com.ex_tickets;

 

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
public Spinner m_spnStart;
public Spinner m_spnEnd;
public Button m_btnRun;
public EditText m_edt01;
public TextView m_txt01;
public RadioButton m_rb01;
public RadioButton m_rb02;
public RadioButton m_rb03;
public RadioGroup m_rgroup;
public String[] m_strPark = {“台北”, “台中”, “高雄”};
private Context mContext;
private ArrayAdapter<String> m_listPark;
public String m_strOut;
public String m_strBuf01,m_strBuf02,m_strBuf03;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this.getApplicationContext();
m_listPark = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, m_strPark);
m_spnStart = (Spinner)findViewById(R.id.spinner1);
m_spnEnd = (Spinner)findViewById(R.id.spinner2);
m_btnRun=(Button)findViewById(R.id.button1);
m_edt01=(EditText)findViewById(R.id.editText1);
m_txt01=(TextView)findViewById(R.id.textView4);
m_rb01=(RadioButton)findViewById(R.id.radio0);
m_rb02=(RadioButton)findViewById(R.id.radio1);
m_rb03=(RadioButton)findViewById(R.id.radio2);
m_rgroup=(RadioGroup)findViewById(R.id.radioGroup1);
m_rgroup.setOnCheckedChangeListener(listener);
m_spnStart.setAdapter(m_listPark);
m_spnEnd.setAdapter(m_listPark);
m_btnRun.setOnClickListener(new ButListener());
m_strBuf03=”成人”;
m_spnStart.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(mContext, “你選的是”+m_strPark[arg2], Toast.LENGTH_SHORT).show();
m_strBuf01=m_strPark[arg2];
}
});
m_spnEnd.setOnItemSelectedListener(new OnItemSelectedListener(){

public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(mContext, “你選的是”+m_strPark[arg2], Toast.LENGTH_SHORT).show();
m_strBuf02=m_strPark[arg2];
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
{
switch (checkedId)
{
case R.id.radio0:
m_strBuf03=”成人”;
break;
case R.id.radio1:
m_strBuf03=”老人”;
break;
case R.id.radio2:
m_strBuf03=”兒童”;
break;
}
}
};

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class ButListener implements OnClickListener
{
public void onClick(View v)
{
m_strOut=m_strBuf03+”:”+m_strBuf01+”~”+m_strBuf02+”共”+m_edt01.getText().toString()+”張數”;
m_txt01.setText(m_strOut);
}
}
}

 

 

 

 


發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *