`
未来程序员
  • 浏览: 25294 次
社区版块
存档分类
最新评论

关于接口在android单选按钮下的实现

 
阅读更多

从接口的定义方面来说,接口其实就是类和类之间的一种协定,一种约束.拿一个例子来说.所有继承了一个接口的类中必需实现接口定义的方法.那么从用户(使用类的用户)的角度来说,如果他知道了某个类是继承于这个接口,那么他就可以放心大胆的调用接口中的方法,而不用管方法怎么具体实现。


用接口目的是方便统一管理.另一个是方便调用.当然了,不使用接口一样可以达到目的.只不过这样的话,这种约束就不那么明显,如果这样类还有Duck类等等,比较多的时候难免有人会漏掉这样方法.所以说还是通过接口更可靠一些,约束力更强一些.

 

 下面用一个安卓的例子来实现接口

这是一个关于回家方案选择的一个接口

 

package com.example.gohome;

public interface ToHome {
	
	public String goHome();

}

 下面是 接口的实现

package com.example.gohome;

public class ByBus implements ToHome {

	@Override
	public String goHome() {

		return "time = 24min ; money = 0.4";
		
	}


}

 

package com.example.gohome;

public class ByAirplane implements ToHome {

	@Override
	public String goHome() {

		return "time = 5min ; money = 998";
		
	}

}

 还有 2种差不多 就不例举了。

 

 

然后我们看下如何定义安卓的单选按钮

 

<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"
    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" >

    <TextView
         android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
    
    <RadioGroup 
        android:id="@+id/RadioGroup01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_x="3dp"
        android:layout_y="54dp"
        >
        
    <RadioButton 
        android:id="@+id/RadioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/RadioButton1"
        />   
     <RadioButton 
        android:id="@+id/RadioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/RadioButton2"
        />   
        
     <RadioButton 
        android:id="@+id/RadioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/RadioButton3"
        />   
        
     <RadioButton 
        android:id="@+id/RadioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/RadioButton4"
        />   
        
        
    </RadioGroup>

</LinearLayout>

 

    关于单选按钮这个会在下篇博客中具体说明,先了解下如何定义

   

    最后看下如何使用接口定义的类

package com.example.gohome;

import java.security.PublicKey;

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.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	
	static ToHome mytohome;
	TextView m_TextView;
	RadioGroup m_RadioGroup;
	RadioButton m_Radio1,m_Radio2,m_Radio3,m_Radio4;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		m_TextView = (TextView)findViewById(R.id.TextView1);
		m_RadioGroup = (RadioGroup)findViewById(R.id.RadioGroup01);
		m_Radio1 = (RadioButton)findViewById(R.id.RadioButton1);
		m_Radio2 = (RadioButton)findViewById(R.id.RadioButton2);
		m_Radio3 = (RadioButton)findViewById(R.id.RadioButton3);
		m_Radio4 = (RadioButton)findViewById(R.id.RadioButton4);
		
		m_RadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId == m_Radio1.getId())
				{
					mytohome = new ByFoot();
					System.out.println(mytohome.goHome());
					DisplayToast(mytohome.goHome());
				}
				if(checkedId == m_Radio2.getId())
				{
					mytohome = new ByBus();
					System.out.println(mytohome.goHome());
					DisplayToast(mytohome.goHome());
				}
				if(checkedId == m_Radio3.getId())
				{
					mytohome = new BySubway();
					System.out.println(mytohome.goHome());
					DisplayToast(mytohome.goHome());
				}
				if(checkedId == m_Radio4.getId())
				{
					mytohome = new ByAirplane();
					System.out.println(mytohome.goHome());
					DisplayToast(mytohome.goHome());
				}
			}

		
				
				
		});
		
	}

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

	private void DisplayToast(String str) {
		Toast toast = Toast.makeText(this, str, Toast.LENGTH_SHORT);
		
		toast.setGravity(Gravity.TOP,0,220);
		
		toast.show();
		
	}
}

 

 

  • 大小: 21.1 KB
  • 大小: 19.9 KB
分享到:
评论

相关推荐

    Android应用开发入门教程

    8.3.1.单选按钮组95 8.3.2.使用滚动条96 8.4 布局(Layout99 8.4.1.基本的布局内容99 8.4.2.线性布局(LinearLayout)100 8.4.3.相对布局(RelativeLayout)101 8.4.4.表单布局(Table Layout)103 8.5 网格...

    android开发秘籍

    4.4.3 秘诀37:使用单选按钮 90 4.4.4 秘诀38:创建下拉菜单 90 4.4.5 秘诀39:使用进度条 92 4.4.6 秘诀40:使用拖动条 94 第5 章 用户界面事件 97 5.1 事件处理器和事件监听器 97 5.1.1 秘诀41:截取物理按键...

    疯狂Android讲义源码

     2.3.4 单选按钮(RadioButton)和复选  框(CheckBox)介绍与应用 69  2.3.5 状态开关按钮(ToggleButton)  的功能与用法 71  2.3.6 时钟(AnalogClock和Digital  Clock)的功能与功法 73  2.3.7 图像视图...

    疯狂Android讲义.part2

    2.3.4 单选按钮(RadioButton)和复选 框(CheckBox)介绍与应用 69 2.3.5 状态开关按钮(ToggleButton) 的功能与用法 71 2.3.6 时钟(AnalogClock和Digital Clock)的功能与功法 73 2.3.7 图像视图(ImageView)的...

    疯狂Android讲义.part1

    2.3.4 单选按钮(RadioButton)和复选 框(CheckBox)介绍与应用 69 2.3.5 状态开关按钮(ToggleButton) 的功能与用法 71 2.3.6 时钟(AnalogClock和Digital Clock)的功能与功法 73 2.3.7 图像视图(ImageView)的...

    Android典型技术模块开发详解

    7.7 RadioButton(单选按钮) 7.8 CheckBox(多选框) 7.9 Spinner(下拉列表) 7.10 AutoCompleteTextview(自动完成) 7.11 DataPicker&TimePicker;(日期&时间) 7.12 ProgressBar (进度条) 7.12.1 横向进度条 ...

    Android 应用开发入门教程(经典版)

    2.2.5. 在Eclipse 中配置Android SDK ......................................................................................................... 20 2.3 Android 中运行仿真器环境...............................

    kodi-18.4-Leia-x86.exe

    修复单选按钮文本长度(Estuary) 修复视频插件的季节/剧集格式(视频) 不要认为显示模式ids不变(Android) 播放/显示 修复PlayMedia内置播放列表(.strm)和“艺术家”智能播放列表(音乐) 修复PlayMedia...

    kodi-18.4-Leia-x64.exe

    修复单选按钮文本长度(Estuary) 修复视频插件的季节/剧集格式(视频) 不要认为显示模式ids不变(Android) 播放/显示 修复PlayMedia内置播放列表(.strm)和“艺术家”智能播放列表(音乐) 修复PlayMedia...

    Android应用开发入门教程(经典版)

    2.2.5. 在Eclipse中配置Android SDK.........................................................................................................20 2.3 Android中运行仿真器环境...................................

    RoboTarPC:RoboTar 机器人吉他助手 - PC 版代码

    能够使用单选按钮构建和弦(TODO - 需要创建保存和弦并将其添加到歌曲的能力) 能够驱动连接到 16 通道 PWM 控制器的 RoboTar 设备(驱动伺服系统)上的和弦。 能够通过连接到 IOIO 板(引脚 34)的物理按钮打开...

    Let's JusTalk to the Web-crx插件

    该扩展目前支持页面导航,可单击的小部件(即链接和按钮-复选框和单选按钮)的概念; 以及价值持有的小部件,即输入文本框和文本区域。 支持的典型命令如下。 扩展本身提供了最新的描述-只需问“我如何与此页面交互...

Global site tag (gtag.js) - Google Analytics