2016-04-28 10 views
2

私はAndroidアプリを開発しています。私は 'About'メニューに項目を作成しました。別のページやアクティビティをクリックすると、Dev Name、Contact、Appバージョンのようなテキストを表示します。私はこれを行う方法を知らない、私はこれらの詳細を表示するために乾杯のメッセージを使用しています。助けてください !Android - メニュー項目にテキストを追加するにはどうすればよいですか?

activity_main.xml

<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" 
    tools:context=".MainActivity" > 

    <ImageButton 
     android:id="@+id/btnSwitch" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="100dip" 
     android:src="@drawable/btn_switch_on" 
     android:background="@null" 
     android:contentDescription="@null" 
     /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/text1" 
     android:layout_marginTop="10dp" 
     android:paddingLeft="10dp" 
     android:lines="2" 
     android:id="@+id/text2" 
     android:clickable="true" 
     android:text="wTorch v1.0" 
     android:fontFamily="Droid Sans" 
     android:autoLink="all" 
     android:typeface="serif" 
     android:textSize="20sp" 
     android:gravity="right" 
     android:textAlignment="gravity" /> 

</RelativeLayout> 

MainActivity.java

package com.wtech.wtorch; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.content.DialogInterface; 
import android.content.pm.PackageManager; 
import android.hardware.Camera; 
import android.hardware.Camera.Parameters; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnCompletionListener; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageButton; 
import android.widget.Toast; 
import android.view.Menu; 
import android.view.MenuItem; 


    public class MainActivity extends Activity { 

    private static final Parameters para = null; 

    private static Camera cam = null; 

    private static boolean doubleBackToExitPressedOnce = false; 

    ImageButton btnSwitch; 

    private Camera camera; 
    private boolean isFlashOn; 
    private boolean hasFlash; 
    Parameters params; 
    MediaPlayer mp; 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.about: 
       Toast.makeText(getApplicationContext()," App Version - 1.0 \n Developer - Mohammad Waris Ansari \n Contact - [email protected]",Toast.LENGTH_LONG).show(); 
      return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 


    @Override 
    public void onBackPressed() { 
     if (doubleBackToExitPressedOnce) { 
      super.onBackPressed(); 
      return; 
     } 

     this.doubleBackToExitPressedOnce = true; 
     Toast.makeText(this, "Press again to exit", Toast.LENGTH_SHORT).show(); 

     new Handler().postDelayed(new Runnable() { 

      @Override 
      public void run() { 
       doubleBackToExitPressedOnce=false;      
      } 
     }, 2000); 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     btnSwitch = (ImageButton) findViewById(R.id.btnSwitch);  
     hasFlash = getApplicationContext().getPackageManager() 
       .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); 

     if (!hasFlash) { 
      AlertDialog alert = new AlertDialog.Builder(MainActivity.this) 
        .create(); 
      alert.setTitle("Error"); 
      alert.setMessage("Sorry, your device doesn't support flash light!"); 
      alert.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        // closing the application 
        finish(); 
       } 
      }); 
      alert.show(); 
      return; 
     } 

     // get the camera 
     getCamera(); 

     // displaying button image 
     toggleButtonImage(); 


     // Switch button click event to toggle flash on/off 
     btnSwitch.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       if (isFlashOn) { 
        // turn off flash 
        turnOffFlash(); 
       } else { 
        // turn on flash 
        turnOnFlash(); 
       } 
      } 
     }); 
    } 


    // Get the camera 
    private void getCamera() { 
     if (camera == null) { 
      try { 
       camera = Camera.open(); 
       params = camera.getParameters(); 
      } catch (RuntimeException e) { 
       Log.e("Camera Error. Failed to Open. Error: ", e.getMessage()); 
      } 
     } 
    } 
// Turning On flash 
    private void turnOnFlash() { 
     if (!isFlashOn) { 
      if (camera == null || params == null) { 
       return; 
      } 
      params = camera.getParameters(); 
      params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
      camera.setParameters(params); 
      camera.startPreview(); 
      isFlashOn = true; 

      // changing button/switch image 
      toggleButtonImage(); 
     } 

    } 


    // Turning Off flash 
    private void turnOffFlash() { 
     if (isFlashOn) { 
      if (camera == null || params == null) { 
       return; 
      } 
      params = camera.getParameters(); 
      params.setFlashMode(Parameters.FLASH_MODE_OFF); 
      camera.setParameters(params); 
      camera.stopPreview(); 
      isFlashOn = false; 

      // changing button/switch image 
      toggleButtonImage(); 
     } 
    } 

    /* 
    * Toggle switch button images 
    * changing image states to on/off 
    * */ 
    private void toggleButtonImage(){ 
     if(isFlashOn){ 
      btnSwitch.setImageResource(R.drawable.btn_switch_on); 
     }else{ 
      btnSwitch.setImageResource(R.drawable.btn_switch_off); 
     } 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     turnOffFlash(); 
    } 

    @Override 
    protected void onRestart() { 
     super.onRestart(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if(hasFlash) 
      turnOnFlash(); 
    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 
     getCamera(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     if (camera != null) { 
      camera.release(); 
      camera = null; 
     } 
    } 

} 

menu_main.xml

+0

は別々の活動を作成し、あなたも –

答えて

1

:NewActivityで

、あなたはこのように古い活動から来ているデータを取得することができます。

dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#FFF" 
    android:text="App Version - 1.0"/> 

<TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:textColor="#FFF" 
    android:text="Developer - Mohammad Waris Ansari"/> 

<Button 
    android:id="@+id/declineButton" 
    android:layout_width="100px" 
    android:layout_height="wrap_content" 
    android:text=" OK " 
    android:layout_marginTop="5dp" 
    android:layout_marginRight="5dp" 
    /> 

</RelativeLayout> 

そして、あなたのonOptionsItemSelectedで、

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.about: 

    // Create custom dialog object 
      final Dialog dialog = new Dialog(MainActivity.this); 
      // Include dialog.xml file 
      dialog.setContentView(R.layout.dialog); 
      // Set dialog title 
      dialog.setTitle("About"); 

      dialog.show(); 

      Button declineButton = (Button) dialog.findViewById(R.id.declineButton); 
      // if decline button is clicked, close the custom dialog 
      declineButton.setOnClickListener(new OnClickListener() { 
       @Override 
       public void onClick(View v) { 
        // Close dialog 
        dialog.dismiss(); 
       } 
      }); 

      return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 
+0

アイテムが重複している正しい緑の記号で答えupvoteすることができます@OggyCoderその活動 –

+0

LinearLayoutを使用 –

1
<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
<item 
android:id="@+id/about" 
android:title="About" 
android:icon="@drawable/ic_menu_info_details"/> 
</menu> 

第二活性を作成しonOptionsItemSelected

でそれにナビゲート
1

ボタンをクリックすると新しいアクティビティを開くことができます。このようなあなたのonOptionsItemSelected(MenuItem item)を更新します。

@Override 
      public boolean onOptionsItemSelected(MenuItem item) { 
       switch (item.getItemId()) { 
        case R.id.about: 
         Intent intent = new Intent(MainActivity.this,NewActivity.class); // code to start new activity instead of showing toast 
         intent.putExtra("version","1.0");      
         startActivity(intent); 
        return true; 
        default: 
         return super.onOptionsItemSelected(item); 
       } 
      } 

あなたはintent.putExtre()を使用して別のアクティビティからのアプリのバージョンは、開発者や連絡先などのデータを送信し、新しい活動にこれらのデータを表示することができます。あなたはカスタムダイアログ警告を表示する必要があります

Intent intent = getIntent(); 
    String version= intent.getStringExtra("version"); 
+0

を起動、 助けてください。 –

関連する問題