美文网首页
安卓开发之radiobutton

安卓开发之radiobutton

作者: bluewind1230 | 来源:发表于2018-01-17 22:11 被阅读0次
image.png
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="20dp"
    tools:context="com.example.my_radiobutton.MainActivity">


    <RadioGroup
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:id="@+id/Radio1"
            android:text="管理员"
            android:onClick="onRadio1Click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
        <RadioButton
            android:id="@+id/Radio2"
            android:text="普通用户"
            android:onClick="onRadio2Click"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </RadioGroup>
    <!--<TextView-->
        <!--android:layout_width="wrap_content"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:text="Hello World!"-->
        <!--app:layout_constraintBottom_toBottomOf="parent"-->
        <!--app:layout_constraintLeft_toLeftOf="parent"-->
        <!--app:layout_constraintRight_toRightOf="parent"-->
        <!--app:layout_constraintTop_toTopOf="parent" />-->

</LinearLayout>

package com.example.my_radiobutton;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

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

    public  void onRadio1Click(View view){
        //使用土司弹出信息:
        RadioButton radioButton = findViewById(view.getId());
        String string = (String)radioButton.getText();
        Toast.makeText(MainActivity.this, //要显示到哪一界面上
                string,                             //要显示的信息
                Toast.LENGTH_SHORT).show();        //显示的时间

    }


    public void onRadio2Click(View view) {
        RadioButton radioButton = findViewById(view.getId());
        String string = (String) radioButton.getText();
        Toast.makeText(MainActivity.this,   //要显示到哪一界面上
                string,                       //要显示的信息
                Toast.LENGTH_SHORT).show();  //显示的时间
    }
}

相关文章

网友评论

      本文标题:安卓开发之radiobutton

      本文链接:https://www.haomeiwen.com/subject/illpoxtx.html