这是拍照的一个例子。 射击后为什么按钮3不可见? 拍照后隐藏按钮的正确方法是什么? 请帮助谢谢。 注意:我尝试使用galaxy 3 mini(18190)>>不起作用
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
public class MainActivity extends ActionBarActivity {
private static final int ACTIVITY_START_CAMER_APP = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.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) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showButton1(View view) {
EditText editText1 = (EditText) findViewById(R.id.editText);
editText1.setText("1", TextView.BufferType.EDITABLE);
showhideButton();
}
public void hideButton2(View view) {
EditText editText1 = (EditText) findViewById(R.id.editText);
editText1.setText("0", TextView.BufferType.EDITABLE);
showhideButton();
}
public void showhideButton0(View view) {
showhideButton();
}
public void showhideButton() {
EditText editText1 = (EditText) findViewById(R.id.editText);
Button button3 = (Button) findViewById(R.id.button3);
int val = Integer.parseInt(editText1.getText().toString().trim());
if (val == 1) {
Toast.makeText(this, "hide Button3", Toast.LENGTH_SHORT).show();
button3.setVisibility(View.INVISIBLE);}
else if (val ==0) { Toast.makeText(this, "show Button3", Toast.LENGTH_SHORT).show();
button3.setVisibility(View.VISIBLE);
}
}
public void takePhoto(View view) throws IOException {
Intent callCameraApplicationIntent = new Intent();
callCameraApplicationIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
callCameraApplicationIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(callCameraApplicationIntent, ACTIVITY_START_CAMER_APP);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
EditText txt4 = (EditText) findViewById(R.id.editText);
if (requestCode == ACTIVITY_START_CAMER_APP && resultCode == RESULT_OK) {
txt4.setText("1", TextView.BufferType.EDITABLE);
}
else { return;}
showhideButton();
}
File createImageFile() throws IOException {
File folder = new File(Environment.getExternalStorageDirectory() + "/image1");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
File folder2 = new File(Environment.getExternalStorageDirectory() + "/image1/Original_Images");
boolean success2 = true;
if (!folder2.exists()) {
success2 = folder2.mkdir();
}
File image = new File(Environment.getExternalStorageDirectory() + "/image1/Original_Images/1.jpg");
return image;
}
}
<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">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="show Button3"
android:id="@+id/button1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="122dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="hideButton2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hide Button3"
android:id="@+id/button2"
android:layout_above="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="showButton1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button3"
android:id="@+id/button3"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:layout_below="@+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="58dp"
android:inputType="number"
android:text="0" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="takePhoto"
android:id="@+id/button"
android:onClick="takePhoto"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="showhideButton"
android:id="@+id/button4"
android:onClick="showhideButton0"
android:layout_above="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>