Android學習第二天:Toast(提醒)、Menu(菜單)、Intent的顯式和隱式(包括開啟、適配網站,調用撥號介面等)

來源:互聯網
上載者:User

標籤:


1.Toast提醒
為昨天寫的按鈕程式添加一個提醒,在MainActivity中添加如下代碼:
Button bt1 = (Button) findViewById(R.id.button_1);bt1.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {        Toast.makeText(MainActivity.this, "您點擊了按鈕1", Toast.LENGTH_SHORT).show();    }});

findViewById()方法用於獲得布局檔案檔案中的元素,值通過屬性Id指定。(該返回值是View對象,需要轉成Button對象)setOnClickListener()方法是用於註冊監聽器,點擊執行makeText()方法需要三個參數,第一個是context(toast要求的上下文,活動本身就是一個context),第二個是顯示的內容,第三個是顯示的時常。
運行結果如下:

2.Menu菜單
首先在res目錄下建立一個menu檔案夾,並在檔案內建立一個xml檔案,命名為main
main.xml檔案中代碼如下:
<menu xmlns:android="http://schemas.android.com/apk/res/android">    <item       android:id="@+id/add_item"       android:title="Add"/>    <item       android:id="@+id/remove_item"       android:title="Remove" /></menu>

每個item標籤為定義一個菜單中的選項
在MainActivity中添加如下代碼,重寫onCreatOptionsMenu()方法
public boolean onCreateOptionsMenu(Menu menu){   super.onCreateOptionsMenu(menu);    getMenuInflater().inflate(R.menu.main,menu);   return true;}

getMenuInflater()方法能夠得到MenuInflater對象,再調用inflate()方法可以給當前活動建立菜單。inflate()方法需要兩個參數,第一個是從哪個資源檔來建立菜單,第二個用於指定功能表項目添加到哪個Menu對象中。(返回值true用於將菜單顯示出來)
運行結果如下:
3.為Menu菜單添加監聽器
在MainActivity中添加如下代碼,重寫onOptionsItemSelected()方法
public boolean onOptionsItemSelected(MenuItem item){   switch(item.getItemId()){       case R.id.add_item:            Toast.makeText(this, "您點擊了Add按鈕", Toast.LENGTH_SHORT).show();           break;       case R.id.remove_item:            Toast.makeText(this, "您點擊了Remove按鈕", Toast.LENGTH_SHORT).show();           break;       default:    }   return true;}

通過item.getItemId()方法來判斷點擊的是哪個功能表項目。
運行結果如下(點擊Add):

4.顯式Intent
首先再建立一個Activity,命名為SecondActivity
public class SecondActivity extends AppCompatActivity{    protected void onCreate(Bundle savedInstanceState){       super.onCreate(savedInstanceState);        setContentView(R.layout.activity_second);    }}

在layout中再建立一個xml,命名為activity_second
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   android:layout_width="match_parent"   android:layout_height="match_parent"   android:orientation="vertical">    <TextView       android:layout_width="wrap_content"       android:layout_height="wrap_content"       android:text="@string/hello_world"  /></LinearLayout>


我讓這個新的活動顯示Hello World
最後到AndroidManifest中給新的活動註冊
<activity android:name=".SecondActivity"></activity>

為了能夠使用第二個活動,將MainActivity中的button的監聽器改為:
Button bt1 = (Button) findViewById(R.id.button_1);bt1.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {        Intent intent = new Intent(MainActivity.this,SecondActivity.class);        startActivity(intent);    }});

程式運行結果如下(點擊按鈕Button 1):


Intent()方法需要兩個參數,第一個參數context要求提供各一個啟動活動的上下文,第二個參數class則是指定想要啟動的活動目標。startActivity()方法用於啟動活動,來執行這個Intent。
5.隱式Intent
首先在AndroidManifest中修改剛才註冊的SecondActivity:
<activity android:name=".SecondActivity">    <intent-filter>        <action android:name="bistu.com.test.ACTION_START"/>        <category android:name="android.intent.category.DEFAULT" />    </intent-filter></activity>

然後修改MainActivity中的按鈕監聽器:
Button bt1 = (Button) findViewById(R.id.button_1);bt1.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {        Intent intent = new Intent("bistu.com.test.ACTION_START");        startActivity(intent);    }});

然後運行這個程式,跟剛才的顯式效果一樣。會發現在監聽器中並沒有指定category,實際上預設為DEFAULT。如果在監聽器中加上一句“intent.addCategory( "bistu.com.test.MY_CATEGORY")”,則會報錯,只需要在活動註冊中,添加“< category  android :name= "bistu.com.test.MY_CATEGORY" />”即可。
6.更多隱式Intent用法
①.將按鈕改為點擊開啟網頁,將MainActivity中的監聽器修改為:
bt1.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {        Intent intent = new Intent(Intent.ACTION_VIEW);        intent.setData(Uri.parse("http://www.baidu.com"));        startActivity(intent);    }});

運行結果如下:

(點擊Button後,用瀏覽器開啟了百度的網站)通過Uri.parse()方法將王志字串解析成一個Uri對象,調用Insert的setData()方法將這個Uri對象傳遞進去。
②.在①的基礎上,讓這個程式適配網頁
修改註冊代碼為:
<activity android:name=".SecondActivity">    <intent-filter>        <action android:name="android.intent.action.VIEW"/>        <category android:name="android.intent.category.DEFAULT" />        <data android:scheme="http"/>    </intent-filter></activity>

點擊按鈕後:

可以選擇用該軟體來適配網頁(但是不能使用)
③.調用系統撥號介面
將按鈕的監聽器改為:
bt1.setOnClickListener(new View.OnClickListener() {   @Override   public void onClick(View view) {        Intent intent = new Intent(Intent.ACTION_DIAL);        intent.setData(Uri.parse("tel:10086"));        startActivity(intent);    }});

點擊button後如下:


Android學習第二天:Toast(提醒)、Menu(菜單)、Intent的顯式和隱式(包括開啟、適配網站,調用撥號介面等)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.