0

所以我有一个自定义AlertDialog的应用程序,它有一个Ok和Cancel按钮以及一个EditText对象。当我点击“锻炼名称”按钮时,AlertDialog和软键盘将打开。此时,AlertDialog打开并显示软键盘,如果用户决定按Home键,则应用程序关闭,但软键盘仍显示。从这一点开始,我将如何关闭软键盘以及整个应用程序?这里是我的代码:关闭主屏幕按键上的软键盘

package com.example.test_project; 

import java.util.Calendar; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class NewWorkout extends Activity { 
/** Called when the activity is first created. */ 

private TextView mDateDisplay; 
private Button mPickDate; 
private int mYear; 
private int mMonth; 
private int mDay; 

private TextView mTimeDisplay; 
private Button mTimePicker1; 
private int hour; 
private int minute; 
private String zone; 

static final int DATE_DIALOG_ID = 0; 
static final int TIME_DIALOG_ID = 99; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_new_workout); 
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

    // capture our View elements 
    mDateDisplay = (TextView) findViewById(R.id.dateOfWorkoutTextView); 
    mPickDate = (Button) findViewById(R.id.dateOfWorkoutButton); 
    mTimeDisplay = (TextView) findViewById(R.id.timeOfWorkoutTextView); 
    mTimePicker1 = (Button) findViewById(R.id.timeOfWorkoutButton); 


    // add a click listener to the button 
    mPickDate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      showDialog(DATE_DIALOG_ID); 
     } 
    }); 

    // get the current date 
    final Calendar c = Calendar.getInstance(); 
    mYear = c.get(Calendar.YEAR); 
    mMonth = c.get(Calendar.MONTH); 
    mDay = c.get(Calendar.DAY_OF_MONTH); 

    final Calendar t = Calendar.getInstance(); 
    hour = t.get(Calendar.HOUR_OF_DAY); 
    minute = t.get(Calendar.MINUTE); 

    //set the current time into the textview 



    mTimePicker1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      showDialog(TIME_DIALOG_ID); 
     } 
    }); 

} 

// updates the date in the TextView 
private void updateDisplay() { 
     StringBuilder string1 = new StringBuilder() 
       // Month is 0 based so add 1 
       .append(mMonth + 1).append("-") 
       .append(mDay).append("-") 
       .append(mYear).append(" "); 
    mDateDisplay.setText(string1); 
} 

// the callback received when the user "sets" the date in the dialog 
private DatePickerDialog.OnDateSetListener mDateSetListener = 
     new DatePickerDialog.OnDateSetListener() { 

      public void onDateSet(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) { 
       mYear = year; 
       mMonth = monthOfYear; 
       mDay = dayOfMonth; 
       updateDisplay(); 
      } 
     }; 

private TimePickerDialog.OnTimeSetListener timePickerListener = 
     new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { 
     hour = selectedHour; 
     minute = selectedMinute; 
     updateTimeDisplay(); 
    } 
}; 

//set current time into textView 
private void updateTimeDisplay() { 
    if(hour > 12){ 
     hour -= 12; 
     zone = "PM"; 
    } 
    else 
     zone = "AM"; 

    if(minute >= 10){ 
    mTimeDisplay.setText(new StringBuilder().append(hour) 
       .append(":").append(minute).append(" ").append(zone)); 
    } 
    else 
     mTimeDisplay.setText(new StringBuilder().append(hour) 
       .append(":").append("0").append(minute).append(" ").append(zone)); 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, 
       mDay); 
    case TIME_DIALOG_ID: 
     return new TimePickerDialog(this, timePickerListener, hour, minute, false); 
    } 
    return null; 
} 


public void nameOfWorkout(View view){ 
    AlertDialog.Builder alert = new AlertDialog.Builder(this); 

    alert.setTitle("Enter a Name for This Workout"); 

    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    alert.setView(input); 
    // Prepping the soft keyboard to open with the AlertDialog 
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(input.getWindowToken(), 0); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     String value = input.getText().toString(); 
     TextView edit = (TextView) findViewById(R.id.nameOfWorkoutTextView); 
     edit.setText(value); 
     imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0); 
     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0); 
     } 
    }); 
     //alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    alert.show(); 

    //Causing the soft keyboard to open with the AlertDialog 
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
} 

} 

谢谢!

UPDATE:

package com.example.test_project; 

import java.util.Calendar; 

import com.example.test_project.R.string; 

import android.app.Activity; 
import android.app.AlertDialog; 
import android.app.DatePickerDialog; 
import android.app.Dialog; 
import android.app.TimePickerDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.WindowManager; 
import android.view.inputmethod.InputMethodManager; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.DatePicker; 
import android.widget.EditText; 
import android.widget.TextView; 
import android.widget.TimePicker; 

public class NewWorkout extends Activity { 
/** Called when the activity is first created. */ 

private TextView mDateDisplay; 
private Button mPickDate; 
private int mYear; 
private int mMonth; 
private int mDay; 

private TextView mTimeDisplay; 
private Button mTimePicker1; 
private int hour; 
private int minute; 
private String zone; 

static final int DATE_DIALOG_ID = 0; 
static final int TIME_DIALOG_ID = 99; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_new_workout); 
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 

    // capture our View elements 
    mDateDisplay = (TextView) findViewById(R.id.dateOfWorkoutTextView); 
    mPickDate = (Button) findViewById(R.id.dateOfWorkoutButton); 
    mTimeDisplay = (TextView) findViewById(R.id.timeOfWorkoutTextView); 
    mTimePicker1 = (Button) findViewById(R.id.timeOfWorkoutButton); 


    // add a click listener to the button 
    mPickDate.setOnClickListener(new View.OnClickListener() { 
     public void onClick(View v) { 
      showDialog(DATE_DIALOG_ID); 
     } 
    }); 

    // get the current date 
    final Calendar c = Calendar.getInstance(); 
    mYear = c.get(Calendar.YEAR); 
    mMonth = c.get(Calendar.MONTH); 
    mDay = c.get(Calendar.DAY_OF_MONTH); 

    final Calendar t = Calendar.getInstance(); 
    hour = t.get(Calendar.HOUR_OF_DAY); 
    minute = t.get(Calendar.MINUTE); 

    //set the current time into the textview 



    mTimePicker1.setOnClickListener(new OnClickListener() { 
     public void onClick(View v) { 
      showDialog(TIME_DIALOG_ID); 
     } 
    }); 

} 

// updates the date in the TextView 
private void updateDisplay() { 
     StringBuilder string1 = new StringBuilder() 
       // Month is 0 based so add 1 
       .append(mMonth + 1).append("-") 
       .append(mDay).append("-") 
       .append(mYear).append(" "); 
    mDateDisplay.setText(string1); 
} 

// the callback received when the user "sets" the date in the dialog 
private DatePickerDialog.OnDateSetListener mDateSetListener = 
     new DatePickerDialog.OnDateSetListener() { 

      public void onDateSet(DatePicker view, int year, 
            int monthOfYear, int dayOfMonth) { 
       mYear = year; 
       mMonth = monthOfYear; 
       mDay = dayOfMonth; 
       updateDisplay(); 
      } 
     }; 

private TimePickerDialog.OnTimeSetListener timePickerListener = 
     new TimePickerDialog.OnTimeSetListener() { 
    public void onTimeSet(TimePicker view, int selectedHour, int selectedMinute) { 
     hour = selectedHour; 
     minute = selectedMinute; 
     updateTimeDisplay(); 
    } 
}; 

//set current time into textView 
private void updateTimeDisplay() { 
    if(hour > 12){ 
     hour -= 12; 
     zone = "PM"; 
    } 
    else 
     zone = "AM"; 

    if(minute >= 10){ 
    mTimeDisplay.setText(new StringBuilder().append(hour) 
       .append(":").append(minute).append(" ").append(zone)); 
    } 
    else 
     mTimeDisplay.setText(new StringBuilder().append(hour) 
       .append(":").append("0").append(minute).append(" ").append(zone)); 
} 

@Override 
protected Dialog onCreateDialog(int id) { 
    switch (id) { 
    case DATE_DIALOG_ID: 
     return new DatePickerDialog(this, mDateSetListener, mYear, mMonth, 
       mDay); 
    case TIME_DIALOG_ID: 
     return new TimePickerDialog(this, timePickerListener, hour, minute, false); 
    } 
    return null; 
} 



public void nameOfWorkout(View view){ 
    AlertDialog.Builder nameOfWorkoutAlert = new AlertDialog.Builder(this); 
    nameOfWorkoutAlert.setTitle("Enter a Name for This Workout"); 

    // Set an EditText view to get user input 
    final EditText input = new EditText(this); 
    nameOfWorkoutAlert.setView(input); 
    // Prepping the soft keyboard to open with the AlertDialog 
    final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(input.getWindowToken(), 0); 

    nameOfWorkoutAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int whichButton) { 
     String value = input.getText().toString(); 
     TextView edit = (TextView) findViewById(R.id.nameOfWorkoutTextView); 
     edit.setText(value); 
     imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0); 
     } 
    }); 

    nameOfWorkoutAlert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0); 
     } 
    }); 
    //alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE); 
    nameOfWorkoutAlert.show(); 

    //Causing the soft keyboard to open with the AlertDialog 
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); 
} 

protected void onPause(){ 
    super.onPause(); 
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(input.getWindowToken(), 0); 
} 

public void typeOfWorkout(View view){ 
    final String [] items=new String []{"Weight-lifting","Cardio","Mixture"}; 
    AlertDialog.Builder builder=new AlertDialog.Builder(this); 
    builder.setTitle("Select Today's Workout type"); 

    builder.setItems(items, new android.content.DialogInterface.OnClickListener() { 

    @Override 
    public void onClick(DialogInterface dialog, int which) { 
    // TODO Auto-generated method stub 
    TextView txt=(TextView)findViewById(R.id.typeOfWorkoutTextView); 
    txt.setText(items[which]); 
    } 
    }); 

    builder.show(); 

} 

} 
+0

什么设备是你的测试?绝大多数设备的默认行为会在用户导航时关闭键盘。 – FoamyGuy 2013-05-04 03:15:39

+0

一个Droid Razr ... – 2013-05-05 02:30:59

回答

2

在你,你可以添加以下内容:

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); 
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); 
+0

好吧,我创建了一个onPause方法,但它无法解析我的EditText对象,因为它不在该范围内,如果我使用了正确的术语。我对所有这些都很陌生,但我认为我需要将该EdtiText对象传递给onPause()方法?这里是我现在拥有的: – 2013-05-04 01:38:43

+0

糟糕,对不起,我的新代码在我原来的帖子中,在UPDATE下。 – 2013-05-04 01:41:38

+0

你应该可以直接在你的其他Button和TextView中声明'input',它应该可以工作。 – TronicZomB 2013-05-04 01:47:31