gpt4 book ai didi

Android - 如何向菜单项添加文本?

转载 作者:搜寻专家 更新时间:2023-11-01 08:35:44 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,我在菜单“关于”中创建了一个项目。我希望它在我单击它时在单独的页面或 Activity 中显示诸如开发人员姓名、联系人、应用程序版本之类的文本。我不知道该怎么做,我正在使用 toast 消息来显示这些详细信息。请帮忙!

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>

主 Activity .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 - mohdwaris723@gmail.com",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

<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>

最佳答案

您将需要显示自定义对话框警报。

对话框.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="Some Text"/>

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:text="Some Text"/>

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

关于Android - 如何向菜单项添加文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36906789/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com