我正在创建一个手电筒应用程序...我想在一个 java 文件中链接两个布局...一个布局是当手电筒未点亮时...另一个布局显示一个照明手电筒...我已经研究过了现在,当我按下按钮时,它会切换到其他布局..(发光的)...但是当我单击按钮时不会回到第一个布局...该怎么办?这是代码..
我的 XML1:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/fl"
tools:context=".Flash" >
<Button
android:id="@+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/bu" />
我的 XML2:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/fl1"
android:orientation="vertical" >
<Button
android:id="@id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/bu" />
我的 Flashlight.java 文件(不包括导入):
package com.potapptoes.flashlight;
public class Flash extends Activity implements OnClickListener {
Camera cam = null;
Button ib1;
Parameters para;
PowerManager pm;
WakeLock wl;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "whatever");
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
wl.acquire();
initialize();
ib1.setOnClickListener(this);
}
private void initialize() {
// TODO Auto-generated method stub
ib1 = (Button) findViewById(R.id.ib2);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (cam == null) {
setContentView(R.layout.main2);
cam = Camera.open();
para = cam.getParameters();
para.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(para);
} else {
setContentView(R.layout.main);
para.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(para);
cam.release();
cam = null;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
para.setFlashMode(Parameters.FLASH_MODE_OFF);
cam.setParameters(para);
cam.release();
cam = null;
wl.release();
finish();
}
}