欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

關于接口在android單選按鈕下的實現

系統 1910 0

從接口的定義方面來說,接口其實就是類和類之間的一種協定,一種約束 .拿一個例子來說.所有繼承了一個接口的類中必需實現接口定義的方法.那么從用戶(使用類的用戶)的角度來說,如果他知道了某個類是繼承于這個接口,那么他就可以放心大膽的調用接口中的方法,而不用管方法怎么具體實現。


用接口目的是方便統一管理.另一個是方便調用.當然了,不使用接口一樣可以達到目的.只不過這樣的話,這種約束就不那么明顯,如果這樣類還有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();
		
	}
}

    

?
關于接口在android單選按鈕下的實現
?

關于接口在android單選按鈕下的實現


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 在线欧美日韩国产 | 精品免费国产一区二区三区 | 九九亚洲视频 | 天天干网 | 嫩草影院永久在线播放 | 99久久精约久久久久久清纯 | 国产精品福利片免费看 | 天天操夜夜夜 | 91高清网站| 欧洲男女下面进出的视频 | 日本高清视频网址 | 日韩一区二区三区在线 | 九一国产在线观看免费 | 天天色天天操天天射 | 色综合久久精品中文字幕首页 | 草草浮力影视 | 亚洲人成网站在线播放观看 | 日本天堂一区 | 欧美日韩综合一区 | 91一区二区三区在线观看 | 日韩亚洲欧美在线爱色 | 免费中日高清无专码有限公司 | 成人在线视频在线观看 | 国产精品一区二区久久 | 亚洲精品色| 嫩草嫩草嫩草 | 久久久久久91香蕉国产 | 天天天天综合 | 国产一级特黄毛片在线毛片 | 91青青国产在线观看免费 | 一级做a爰片性色毛片视频图片 | 精品72久久久久久久中文字幕 | 精品在线观看国产 | 亚洲激情一区二区 | 五月丁香啪啪. | 国产第一页浮力 | 中文字幕一区二区精品区 | 亚洲精品一区二区三区在线 | 欧美一级特黄aaaaaaa在线观看 | 免费国产一级特黄久久 | 黄色影院在线看 |