android中Radiobutton的使用模拟一个选择支付的界面

欢迎访问我的blogThe Blog of Dxhua
android中Radiobutton能实现在众多选择中实现单选,是一个很好的控件,在我们平常的开发中很多地方都会涉及到单选择的的场景
但是要实现单一选择,必须要和RadioGroup结合起来用,只有在一个RadioGrou中的RadioButton才能实现单选,否则每个按钮都是可以选择的。

  • 下面先贴出RadioButton的代码
<RadioGroup
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:id="@+id/payWay_choose">
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/alipay"
           android:padding="20dp"
           android:drawableLeft="@drawable/alipay"
           android:text="支付宝支付"/>
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/weChat"
           android:padding="20dp"
           android:drawableLeft="@drawable/wechat"
           android:text="微信支付"/>
   </RadioGroup>

在上面代码中我在一个RadioGroup中实现了两个RadioButton,分别是支付宝支付和微信支付,在其中android:drawable***方法可以来调整图片的位置,这还可以自己来试试,这里就不一一来展示了,的实现出来的效果如下图所示:
[图片上传失败...(image-6487a2-1534263896075)]
在这里就只能单一选择支付方式了,在这里还能讲讲怎么拿到其他apk里面的图片,在我们的日常开发中,我们可能需要某些软件的icon图,
在网上又不好找,当我们找不到是,可以把这个软件的apk下下来,然后将后缀名改成zip压缩文件格式在打开压缩文件,一般的icon图片就
在res文件中,就拿支付宝来举例吧。

  1. 首先将支付宝apk下下来,然后将.apk改成.zip
    [图片上传失败...(image-c46065-1534263896075)][图片上传失败...(image-3129f-1534263896075)]
    2.双击打开,找到res文件打开里面就有我们需要的图片了
    [图片上传失败...(image-cf74e-1534263896075)]
    zhifubaotupian

    3.基本的软件都是在res文件中,但是微信有点不同,他的icon图片在r文件中
    [图片上传失败...(image-3c688f-1534263896075)]
    好了,题外话说完了,继续我们的支付选择界面,下面附上完整的支付界面layout代码
<?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:orientation="vertical"
    tools:context=".MainActivity">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/text"
            android:text="这里可以加图片或者加上支付内容"
           android:layout_gravity="center"/>
    </LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:layout_marginTop="5dp"
        android:background="#000" />

    <LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="2">
   <RadioGroup
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical"
       android:id="@+id/payWay_choose">
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/alipay"
           android:padding="20dp"
           android:drawableLeft="@drawable/alipay"
           android:text="支付宝支付"/>
       <RadioButton
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:id="@+id/weChat"
           android:padding="20dp"
           android:drawableLeft="@drawable/wechat"
           android:text="微信支付"/>

   </RadioGroup>
</LinearLayout>
    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:layout_marginTop="5dp"
        android:background="#000" />

        <TextView
            android:id="@+id/sure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="50dp"
            android:background="#4F94CD"
            android:gravity="center"
            android:paddingBottom="10dp"
            android:paddingTop="10dp"
            android:paddingStart="50dp"
            android:paddingEnd="50dp"
            android:text="确定"
            android:textColor="#CD0000"
            android:textSize="22sp" />

</LinearLayout>

这个布局实现的效果如下图:
[图片上传失败...(image-623912-1534263896075)]
接下来是一些简单的逻辑书写,实现选择不同的RadioButton实现不同的支付方式

package com.dxhua.payuiradiobuttontest;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
   private RadioGroup radioGroup;
   private RadioButton alipay;
   private RadioButton weChat;
   private int payWay = 0;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       initView();
       alipay = (RadioButton)findViewById(R.id.alipay);
       weChat = (RadioButton)findViewById(R.id.weChat);
       radioGroup = (RadioGroup)findViewById(R.id.payWay_choose);
       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(RadioGroup radioGroup, int i) {
               if (alipay.getId()==i){
                   payWay = 1;
               }
               if (weChat.getId()==i){
                   payWay = 2;
               }
           }
       });


   }
public void initView(){
       findViewById(R.id.sure).setOnClickListener(this);
       findViewById(R.id.text).setOnClickListener(this);

}


   @Override
   public void onClick(View view) {
       switch (view.getId()){
           case R.id.sure:
               if (payWay==0){
                   Toast.makeText(this,"请选择支付方式",Toast.LENGTH_LONG).show();
               }else if (payWay==1){
                   Toast.makeText(this,"你选择了支付宝支付",Toast.LENGTH_LONG).show();
               }else if (payWay==2){
                   Toast.makeText(this,"你选择了微信支付",Toast.LENGTH_LONG).show();
               }
               break;
       }

   }
}

这样一个简单的支付选择界面就完成了,当然我们也可以加入其它的支付方式 效果如下,选择不同的支付方式,去往不同支付界面
[图片上传失败...(image-ef88a0-1534263896075)]
[图片上传失败...(image-f9fde2-1534263896075)]
[图片上传失败...(image-752ac5-1534263896075)]

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,761评论 5 460
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,953评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,998评论 0 320
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,248评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,130评论 4 356
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,145评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,550评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,236评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,510评论 1 291
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,601评论 2 310
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,376评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,247评论 3 313
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,613评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,911评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,191评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,532评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,739评论 2 335

推荐阅读更多精彩内容