我只是希望当用户点击软键盘上的“完成”键时,alertDialog框会消失。我尝试使用dismiss()方法,但它说无法解析'allet11'这是我的AlertDialog对象。
MainActivity.java
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.content.pm.PackageInfo;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button PicButton = (Button)findViewById(R.id.picButton_ID);
imageView = (ImageView)findViewById(R.id.imageView_ID);
Button topTextButton = (Button)findViewById(R.id.topButton_ID);
Button bottonTextButton = (Button)findViewById(R.id.bottomButton_ID);
final TextView topText = (TextView)findViewById(R.id.topText_ID);
TextView bottomText = (TextView)findViewById(R.id.bottomText_ID);
//Disables button if user has no camera
if(!hasCamera()) {
PicButton.setEnabled(false);
}
final AlertDialog.Builder builder1 = new AlertDialog.Builder(this);
topTextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
builder1.setMessage("Write your zinger here.");
builder1.setCancelable(true);
final EditText input = new EditText(MainActivity.this);
builder1.setView(input);
input.setInputType(InputType.TYPE_CLASS_TEXT);
input.setImeOptions(EditorInfo.IME_ACTION_DONE);
//checks if "Done" button on soft keyboard is clicked
input.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//what happens when "Done" is clicked
topText.setText(input.getText().toString());
alert11.dismiss();
}
return false;
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
}
//checks if user has a camera
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
//Launcing the camera
public void launchCamera(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//take a picture and pass results along to onAcivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
//if you want to return the image taken
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
//get the photo
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap)extras.get("data");
imageView.setImageBitmap(photo);
}
}
}
答案 0 :(得分:0)
使用
final AlertDialog builder1 = new AlertDialog.Builder(this).create();
而不是
final AlertDialog.Builder builder1 = new AlertDialog.Builder(this);