简体   繁体   English

AlertDialog 和软键盘

[英]AlertDialog and soft keyboard

I am developing an android application.我正在开发一个 android 应用程序。 I am using the alertDialog class.我正在使用 alertDialog 类。 Where I have a custom listview and 2 buttons.我有一个自定义列表视图和 2 个按钮。 In my custom listview I have an EditText Field.在我的自定义列表视图中,我有一个 EditText 字段。 But when the dialog pops and I touch the EditText while the cursor starts flashing in the Textbox the softKeyboard doesn't appear.但是当对话框弹出并且我触摸 EditText 而光标开始在文本框中闪烁时,软键盘不会出现。 Is it because of the alertdialog?是因为警报对话框吗?

public class ProgressReport extends ListActivity {

private ResultSet receivedData;
private StudentProgressAdapter ca;
private Button btnRtn;
ArrayList<String> content =new ArrayList<String>();
ArrayList<String> subContent = new ArrayList<String>();
AlertDialog dialog ;


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);     
        setTheme(android.R.style.Theme_Holo_Light);         
        setContentView(R.layout.examslayout);
        

        
        for(int i =0;i<20;i++)
          content.add("ELEMENTARY: "+i);
        
          for(int i =0;i<20;i++)
              subContent.add("ΒΑΣΙΚΗ ΚΑΤΗΓΟΡΙΑ");
          
          
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Student Stats").setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, close
                    // current activity
                    //MainActivity.this.finish();
                }
              })
            .setNegativeButton("No",new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,int id) {
                    // if this button is clicked, just close
                    // the dialog box and do nothing
                    dialog.cancel();
                }
            });;

            ListView modeList = new ListView(this);

            modeList.setAdapter(new ProgressReportAdapter2(this, new int[]{R.layout.arrowlistview}, new int[]{R.id.textView1,R.id.textView2},content,20,subContent));

            builder.setView(modeList);
            
            dialog = builder.create();
            dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            
        Intent intent = getIntent();      
        receivedData =  (ResultSet) intent.getSerializableExtra("SentData");
        
        ArrayList<String> content = new ArrayList<String>(MainPagerActivity.receivedData.getStudents().length);
        for (int i=0; i <receivedData.getStudents().length; i++)
              content.add(receivedData.getStudents()[i].getProperty(3).toString()+" "+receivedData.getStudents()[i].getProperty(2).toString());
        
        ca= new StudentProgressAdapter(this, new int[]{R.layout.stdprgrlistview}, new int[]{R.id.textView1}, content,content.size());
        setListAdapter(ca); 
        
        btnRtn= (Button) findViewById(R.id.btn);
        btnRtn.setOnClickListener(new OnClickListener() {            
            public void onClick(View v) {     
                finish();
            }
        });
    }
 
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {    
        dialog.show();
    }

Try this:尝试这个:

AlertDialog alertToShow = alert.create();
alertToShow.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
alertToShow.show();

Update:更新:

you can modify your code like this你可以像这样修改你的代码

dialog = builder.create();
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
dialog.show();

Hope this helps.希望这可以帮助。

创建对话框后添加:

dialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM