如何在Edittext popup Android上打开键盘

时间:2013-09-22 16:40:10

标签: android

我有一个问题弹出窗口。 我无法在EditText上输入弹出窗口。当我按下TextView时,我想打开弹出窗口并在按下Save按钮时键入EditText以获得TextView内容Edittext

班级弹出打开!!!

private TextView name;
private PopupWindow popupWindow=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        name=(TextView)findViewById(R.id.name);
        name.setOnClickListener(new OnClickListener()
        { @Override
            public void onClick(View arg0) {
                if(popupWindow!=null)
                {
                    popupWindow.dismiss();
                    popupWindow=null;
                }
                openpopup();
            }
        });
    }

功能打开弹出窗口

private void openpopup()
{
    LayoutInflater layoutInflater= getLayoutInflater();
    View popupView = layoutInflater.inflate(R.layout.edittext, null);
    popupWindow = new PopupWindow(popupView,LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
    Button btnsave = (Button)popupView.findViewById(R.id.save);
    Button btncancel =(Button)popupView.findViewById(R.id.cancel);
    final EditText text=(EditText)popupView.findViewById(R.id.name);
    btnsave.setOnClickListener(new Button.OnClickListener(){
    @Override
      public void onClick(View v) {
         popupWindow.dismiss();
         popupWindow=null;
         if(text.getText()==null)
         {
         name.setText(text.getText().toString());
         }
      }}); 
    btncancel.setOnClickListener(new Button.OnClickListener(){
    @Override
      public void onClick(View v) {
         popupWindow.dismiss();
         popupWindow=null;
      }}); 
    popupWindow.showAsDropDown(name, 0, 0);
}

activity_main.xml中

<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"
    tools:context=".MainActivity" >
    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:text="@string/name" />
</RelativeLayout>

edittext.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="text" >
        <requestFocus />
    </EditText>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+string/save"/>
         <Button
            android:id="@+id/cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@+string/cancel"/>
    </LinearLayout>
</LinearLayout>

如何打开键盘,您可以输入弹出的Edittext?

1 个答案:

答案 0 :(得分:0)

您可以通过请求焦点到EditText

来完成此操作

如果您请求焦点到editText,键盘将自动出现