• radioGroup


    package com.example.radiogroup_06;

    import android.app.Activity;
    import android.os.Bundle;
    import android.view.Gravity;
    import android.view.Menu;
    import android.widget.RadioButton;
    import android.widget.RadioGroup;
    import android.widget.RadioGroup.OnCheckedChangeListener;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {

    private TextView textView;
    private RadioGroup radioGroup;
    private RadioButton radioBtn01;
    private RadioButton radioBtn02;
    private RadioButton radioBtn03;
    private RadioButton radioBtn04;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView = (TextView) findViewById(R.id.text);
    radioGroup = (RadioGroup) findViewById(R.id.radio_group);
    radioBtn01 = (RadioButton) findViewById(R.id.radio_btn01);
    radioBtn03 = (RadioButton) findViewById(R.id.radio_btn02);
    radioBtn03 = (RadioButton) findViewById(R.id.radio_btn03);
    radioBtn04 = (RadioButton) findViewById(R.id.radio_btn04);

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
    public void onCheckedChanged(RadioGroup group,int checkedId){
    if(checkedId==radioBtn03.getId()){
    displayToast("正確答案:"+radioBtn03.getText()+" "+"恭喜您!回答正確");
    }else{
    displayToast("回答錯誤");
    }
    }


    });
    }


    public void displayToast(String string){
    Toast toast = Toast.makeText(this, string, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.BOTTOM, 0, 220);
    toast.show();
    }

    @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;
    }

    }

    XML文件:

    <LinearLayout 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"
    android:orientation="vertical">

    <TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/text" />
    <RadioGroup
    android:id="@+id/radio_group"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <RadioButton
    android:id="@+id/radio_btn01"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn01"/>
    <RadioButton
    android:id="@+id/radio_btn02"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn02"/>
    <RadioButton
    android:id="@+id/radio_btn03"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn03"/>
    <RadioButton
    android:id="@+id/radio_btn04"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/radio_btn04"/>
    </RadioGroup>

    </LinearLayout>

  • 相关阅读:
    C# 注册表操作类
    NVelocity for AS“.NET研究”P.NET MVC 狼人:
    C#4.0新特性&quot;协变&quot;“.NET研究”与&quot;逆变&quot;以及背后的编程思想 狼人:
    原创“.NET研究”企业级控件库之图片浏览控件 狼人:
    “.NET研究”关于C# 中的Attribute 特性 狼人:
    如何让ASP.NET默认的资源编程“.NET研究”方式支持非.ResX资源存储 狼人:
    使用 “.NET研究”IIS Express 取代 ASP.NET Development Server 狼人:
    云计算从基础到应用架“.NET研究”构系列云计算的演进 狼人:
    通过自定义配置实现插“.NET研究”件式设计 狼人:
    ASP.NET MVC 3 概述“.NET研究” 狼人:
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4302000.html
Copyright © 2020-2023  润新知