Android 点击按钮,实现文字,背景转变;点击单选按钮出现文字提示

<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:orientation="vertical"
    tools:context=".MainActivity">
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/dingyue"
    android:background="@color/colorred"
    android:onClick="onclick"
    android:id="@+id/bt_red"
    />
<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/rgp">
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb_nan"
        android:text="@string/nan"
        android:textSize="25sp"
        />
    <!-- checked是默认选择的-->
    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/rb_nv"
        android:text="@string/nv"
        android:textSize="25sp"
        android:checked="true"
        />
</RadioGroup>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt_submit"
        android:text="提交"
        android:textSize="25sp"
        />
</LinearLayout>

package com.example.syx.qiangguodemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    //为bt_red设置全局变量
    private Button bt_red;
    private RadioButton rb_nan;
    private RadioButton rb_nv;
    private Button bt_submit;
    private RadioGroup rgp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //找到bt_red按钮用findViewById方法
        bt_red=(Button)findViewById(R.id.bt_red);
        //找到rb_nan按钮用findViewById方法
        rb_nan=(RadioButton)findViewById(R.id.rb_nan);
        //找到rb_nv按钮用findViewById方法
        rb_nv=(RadioButton)findViewById(R.id.rb_nv);
        //找到bt_submit用findViewById()方法
        bt_submit=(Button)findViewById(R.id.bt_submit);

        rgp=(RadioGroup)findViewById(R.id.rgp);


        //给两个单选按钮设置事件监听,用setOnCheckedChangeListener(); 继承oncheckedchangelintener接口,生成一个方法
        rgp.setOnCheckedChangeListener(this);

        //给bt_submit添加事件监听器  链接接口  生成onClick方法;
        bt_submit.setOnClickListener(this);




    }
        //添加点击方法,下面方法名onclick必需和.xml文件下的按钮OnClick方法保持一致
    public void onclick(View v){
        //改变按钮的文本内容
        bt_red.setText("已订阅");
        //改变按钮的背景颜色,调用了,资源文件下的颜色
        bt_red.setBackgroundColor(getResources().getColor(R.color.colorgry));
        //弹出框,添加提示文本,Toast.makeText(xml文件的名字,“提示信息”,弹出框的显示时间长度).show();必须得调用show方法。
        Toast.makeText(MainActivity.this,"您已订阅本文",Toast.LENGTH_LONG).show();

    }


    @Override
    public void onClick(View v)  {
//       if(){
//           Toast.makeText(MainActivity.this,"男",Toast.LENGTH_SHORT).show();
//       }
//        if(){
//           Toast.makeText(MainActivity.this,"女",Toast.LENGTH_SHORT).show();
//       }
    }


    //获取单选框的按钮选择了什么
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        if(checkedId==R.id.rb_nan){

           Toast.makeText(MainActivity.this,"您选择的性别是男",Toast.LENGTH_SHORT).show();
        }
        if(checkedId==R.id.rb_nv){

            Toast.makeText(MainActivity.this,"您选择的性别是女",Toast.LENGTH_SHORT).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.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }



}

发布了57 篇原创文章 · 获赞 1 · 访问量 981

猜你喜欢

转载自blog.csdn.net/qq_45844648/article/details/104927992