1

こんにちは、完璧な土曜日のコーディングのために:)ナビゲーション引き出し:アイテムをウェブサイトに誘導する方法。 Androidスタジオ

私はアイテムをクリックすることができます。しかし、ナビゲーション・ドロワーの項目を押して私をウェブサイトにリンクさせると、どうすればいいのですか?私はアイテムでは本当に良いではないよ...

私はだからあなたは、それを押したときの例は、3枚の異なるimgur画像See the 3 images here を掲載します:www.google.comはあなたの通常の「クロームアプリ」で起動します私は本当にないんだけどウェブサイトのウェブビューの原因に興味があるのはhtml5ではありません。

これは、私のフラグメントのアイテムをどのように使用するのかを見つけることができる、私のMainActivityです。

私はルーキープログラマーheheです...しかし、それはとても楽しいです!ステップバイステップで学習。

public class MainActivity extends AppCompatActivity 
 

 

 
     implements NavigationView.OnNavigationItemSelectedListener { 
 
//TextView tstnr; 
 
    TextView radertst; 
 

 
    Button sendSMSaon; 
 
    EditText aonTxt; 
 
    //TextView nrladd; 
 

 

 

 

 

 

 

 

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

 
     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
 
     setSupportActionBar(toolbar); 
 
     FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
 
     fab.setOnClickListener(new View.OnClickListener() { 
 
      @Override 
 
      public void onClick(View view) { 
 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
 
         .setAction("Action", null).show(); 
 
      } 
 
     }); 
 

 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
     ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
 
       this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); 
 
     drawer.setDrawerListener(toggle); 
 
     toggle.syncState(); 
 

 
     NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); 
 
     navigationView.setNavigationItemSelectedListener(this); 
 

 

 
     // tstnr = (TextView) findViewById(R.id.nummertestsp); 
 
     radertst = (TextView) findViewById(R.id.raderanumtxt); 
 

 
     sendSMSaon = (Button)findViewById(R.id.skickaaon); 
 
     aonTxt = (EditText)findViewById(R.id.aon); 
 
    // nrladd = (TextView)findViewById(R.id.numretladd); 
 

 

 
    } 
 
//This is where the call for the value in the setttings are. 
 
//Här är så att man kan lägga in values från inställningar till mainactivity. 
 

 
    public void displayData(View view){ 
 

 
     SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this); 
 
    String name = prefs.getString("example_text", ""); 
 
    radertst.setText(name + " "); 
 
    } 
 

 
//downbelow is where the onresume so the value boots up with the app. 
 
//nedanför är för att appen ska ladda koden i settings direkt när man startar 
 

 
    @Override 
 
    protected void onResume() { 
 
     super.onResume(); 
 
     SharedPreferences prefs =PreferenceManager.getDefaultSharedPreferences(this); 
 

 
     String name = prefs.getString("example_text", ""); 
 
     radertst.setText(name + " ");} 
 

 

 

 
    @Override 
 
    public void onBackPressed() { 
 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
     if (drawer.isDrawerOpen(GravityCompat.START)) { 
 
      drawer.closeDrawer(GravityCompat.START); 
 
     } else { 
 
      super.onBackPressed(); 
 
     } 
 
    } 
 

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

 
    @Override 
 
    public boolean onOptionsItemSelected(MenuItem item) { 
 
     // Handle action bar item clicks here. The action bar will 
 
     // automatically handle clicks on the Home/Up button, so long 
 
     // as you specify a parent activity in AndroidManifest.xml. 
 
     int id = item.getItemId(); 
 

 
     //noinspection SimplifiableIfStatement 
 
     if (id == R.id.action_settings) { 
 
      Intent intent = new Intent(this, SettingsActivity.class); 
 
      startActivity(intent); 
 

 

 

 
      return true; 
 
     } 
 

 

 

 

 
     return super.onOptionsItemSelected(item); 
 
    } 
 

 

 
    @SuppressWarnings("StatementWithEmptyBody") 
 
    @Override 
 
    public boolean onNavigationItemSelected(MenuItem item) { 
 
     // Handle navigation view item clicks here. 
 
     int id = item.getItemId(); 
 
     android.app.FragmentManager fragmentManager = getFragmentManager(); 
 

 

 
     if (id == R.id.nav_first_layout) { 
 

 
      fragmentManager.beginTransaction() 
 
        .replace(R.id.content_frame 
 
        , new FirstFragment()) 
 
        .commit(); 
 
      // Handle the camera action 
 
     } else if (id == R.id.nav_second_layout) { 
 
      fragmentManager.beginTransaction() 
 
        .replace(R.id.content_frame 
 
          , new SecondFragment()) 
 
        .commit(); 
 

 
     } else if (id == R.id.nav_third_layout) { 
 
      fragmentManager.beginTransaction() 
 
        .replace(R.id.content_frame 
 
          , new ThirdFragment()) 
 
        .commit(); 
 

 

 

 

 
     } 
 

 

 

 

 
     DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
 
     drawer.closeDrawer(GravityCompat.START); 
 
     return true; 
 

 

 

 

 

 

 

 

 
} 
 
}

答えて

0

だけに

Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
startActivity(browserIntent); 

ので、あなたのメニュー項目にこれを追加するにはonNavigationItemSelectedあなたは場合は別のものを追加し、必要な項目のIDを検索する必要があります:私は例を作っ

 @Override 
    public boolean onNavigationItemSelected(MenuItem item) { 

     int id = item.getItemId(); 

     if (id == R.id.action_settings) { 
      Intent intent = new Intent(this, SettingsActivity.class); 
      startActivity(intent); 

      return true; 
     } 

     else if (id == R.id.YOUR_ITEM_ID) { 
      Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
      startActivity(browserIntent); 

      return true; 
     } 

     return true; 
    } 
+0

ありがとうございます!それは本当にうまくいった!あなたは本当にそこに私を案内してくれました*)*) – Tim

1

あなたはしかし、あなたは、クリックイベントを処理したい、あなたのonClickボタンの方法、またはリスナーにこのようなものが必要:

Intent intent = new Intent("android.intent.action.VIEW", 
       Uri.parse("http://www.google.com/")); 
startActivity(intent); 

あなたがわからない場合クリック自体を処理する方法はcheck this out.ですが、あなたのコードで見ることができるので、それは問題ありません。

関連する問題