Java-Android 之選項按鈕的運用

來源:互聯網
上載者:User

標籤:android   blog   http   io   ar   os   java   for   sp   

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/text"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="選項按鈕" /><RadioGroup     android:id="@+id/sex"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    >    <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text= "男"        ></RadioButton>   <RadioButton        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text= "女"        ></RadioButton></RadioGroup><Button    android:id="@+id/select"    android:text="選擇"    android:layout_width="fill_parent"    android:layout_height="wrap_content"            android:topLeftRadius="5dip"               android:topRightRadius="5dip"               android:bottomLeftRadius="5dip"               android:bottomRightRadius="5dip"    ></Button>        </LinearLayout>

 MainActivity.java

package cn.szy.com;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.RadioButton;import android.widget.RadioGroup;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    /** Called when the activity is first created. */private RadioGroup group;private Button button;private TextView textView;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        group = (RadioGroup) this.findViewById(R.id.sex);        button  =(Button) this.findViewById(R.id.select);        textView = (TextView) this.findViewById(R.id.text);        button.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                // Perform action on click            int len = group.getChildCount();            String msg = "您選擇了";            textView.setText(msg);            //            for(int i=0;i<len;i++){//            RadioButton radiobutton  = (RadioButton) group.getChildAt(i);//            if(radiobutton.isChecked()){//            msg = radiobutton.getText().toString();//            break;//            }//            }            RadioButton radiobutton1  = (RadioButton) group.getChildAt(0);            RadioButton radiobutton2  = (RadioButton) group.getChildAt(1);            if(radiobutton1.isChecked()){            Toast.makeText(MainActivity.this, "男", 1).show();            }else if(radiobutton2.isChecked()){            Toast.makeText(MainActivity.this, "女", 1).show();            }                        }        });    }}

 

Java-Android 之選項按鈕的運用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.