Android-navigation

提供:Dev Guides
移動先:案内検索

Android-ナビゲーション

この章では、アプリケーション間で前後にナビゲーションを提供する方法を説明します。 最初に、アプリケーションでアップナビゲーションを提供する方法を見ていきます。

上ナビゲーションを提供する

上ナビゲーションにより、アプリケーションは次のアクティビティから前のアクティビティに移動できます。 このようにすることができます。

Upナビゲーションを実装するための最初のステップは、各アクティビティに適切な親であるアクティビティを宣言することです。 これを行うには、アクティビティで parentActivityName 属性を指定します。 その構文は以下のとおりです-

android:parentActivityName = "com.example.test.MainActivity"

その後、アクティビティのonCreateメソッドで* getActionBar()の *setDisplayHomeAsUpEnabled メソッドを呼び出す必要があります。 これにより、上部のアクションバーの[戻る]ボタンが有効になります。

getActionBar().setDisplayHomeAsUpEnabled(true);

最後に行う必要があるのは、 onOptionsItemSelected メソッドをオーバーライドすることです。 ユーザーがそれを押すと、アクティビティはonOptionsItemSelected()の呼び出しを受け取ります。 アクションのIDは android.R.id.home です。その構文は以下のとおりです-

public boolean onOptionsItemSelected(MenuItem item) {

   switch (item.getItemId()) {
      case android.R.id.home:
      NavUtils.navigateUpFromSameTask(this);
      return true;
   }
}

ハンドリングデバイスの戻るボタン

[戻る]ボタンを有効にしてアプリケーション内をナビゲートしているため、デバイスの[戻る]ボタンにアプリケーションの閉じる機能を配置することができます。

*onBackPressed* をオーバーライドし、 *moveTaskToBack* および *finish* メソッドを呼び出すことで実行できます。 その構文は以下のとおりです-
@Override
public void onBackPressed() {
   moveTaskToBack(true);
   MainActivity2.this.finish();
}

このsetDisplayHomeAsUpEnabledメソッドとは別に、ActionBar APIクラスで使用できる他のメソッドがあります。 それらは以下にリストされています-

Sr.No Method & description
1

addTab(ActionBar.Tab tab, boolean setSelected)

このメソッドは、タブ付きナビゲーションモードで使用するタブを追加します

2

getSelectedTab()

このメソッドは、タブ付きナビゲーションモードで、少なくとも1つのタブが存在する場合、現在選択されているタブを返します

3

hide()

このメソッドは、現在表示されている場合、ActionBarを非表示にします

4

removeAllTabs()

このメソッドは、アクションバーからすべてのタブを削除し、現在のタブの選択を解除します

5

selectTab(ActionBar.Tab tab)

このメソッドは、指定されたタブを選択します

以下の例は、ナビゲーションの使用方法を示しています。 アプリケーション内をナビゲートできる基本的なアプリケーションを作成します。

この例を試すには、実際のデバイスまたはエミュレーターでこれを実行する必要があります。

Steps Description
1 You will use Android studio to create an Android application under a package com.example.sairamkrishna.myapplication.
2 Modify src/MainActivity.java file to add Activity code.
3 Create a new activity with the name of second_main.java and edit it to add activity code.
4 Modify layout XML file res/layout/activity_main.xml add any GUI component if required.
5 Modify layout XML file res/layout/second.xml add any GUI component if required.
6 Modify AndroidManifest.xml to add necessary code.
7 Run the application and choose a running android device and install the application on it and verify the results.
*src/MainActivity.java* のコンテンツは次のとおりです。
package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity  {
   Button b1;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      b1 = (Button) findViewById(R.id.button);
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent in=new Intent(MainActivity.this,second_main.class);
            startActivity(in);
         }
      });
   }
}
*src/second_main.java* の内容は次のとおりです。
package com.example.sairamkrishna.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

/**
   * Created by Sairamkrishna on 4/6/2015.
*/

public class second_main extends Activity {
   WebView wv;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main_activity2);

      wv = (WebView) findViewById(R.id.webView);
      wv.setWebViewClient(new MyBrowser());
      wv.getSettings().setLoadsImagesAutomatically(true);
      wv.getSettings().setJavaScriptEnabled(true);
      wv.loadUrl("http://www.finddevguides.com");
   }

   private class MyBrowser extends WebViewClient {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
         view.loadUrl(url);
         return true;
      }
   }
}
*activity_main.xml* の内容は次のとおりです。

'_以下のコードで* abc *はfinddevguides.comのロゴを示します_

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin"
   tools:context=".MainActivity"
   android:transitionGroup="true">

   <TextView android:text="Navigation example" android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/textview"
      android:textSize="35dp"
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"/>

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point"
      android:id="@+id/textView"
      android:layout_below="@+id/textview"
      android:layout_centerHorizontal="true"
      android:textColor="#ff7aff24"
      android:textSize="35dp"/>

   <ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageView"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView"
      android:layout_centerHorizontal="true"
      android:theme="@style/Base.TextAppearance.AppCompat"/>

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="first page"
      android:id="@+id/button"
      android:layout_below="@+id/imageView"
      android:layout_alignRight="@+id/textView"
      android:layout_alignEnd="@+id/textView"
      android:layout_marginTop="61dp"
      android:layout_alignLeft="@+id/imageView"
      android:layout_alignStart="@+id/imageView"/>

</RelativeLayout>
*activity_main_activity2.xml* の内容は次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:weightSum="1">

   <WebView
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:id="@+id/webView"
      android:layout_gravity="center_horizontal"
      android:layout_weight="1.03"/>

</LinearLayout>
*Strings.xml* の内容は次のとおりです。
<resources>
   <string name="app_name">My Application</string>
</resources>
*AndroidManifest.xml* のコンテンツは次のとおりです。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.sairamkrishna.myapplication" >
   <uses-permission android:name="android.permission.INTERNET"></uses-permission>

   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >

      <activity
         android:name=".MainActivity"
         android:label="@string/app_name" >

         <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>

      </activity>

      <activity android:name=".second_main"></activity>

   </application>
</manifest>

アプリケーションを実行してみましょう。 環境設定中に AVD を作成したと思います。 Androidスタジオからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[画像を実行:/android/images/eclipse_run.jpg [Eclipse Run Icon]アイコンをクリックします。 AndroidスタジオはAVDにアプリをインストールして起動し、セットアップとアプリケーションで問題がなければ、次のエミュレータウィンドウを表示します。

ボタンを押すだけで、次の画面が表示されます。

Androidナビゲーションチュートリアル

2番目のアクティビティにはWebViewが含まれ、以下に示すようにfinddevguides.comにリダイレクトされています

Androidナビゲーションチュートリアル