1

我有一个问题弹出窗口。我无法在 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>

编辑文本.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?

4

1 回答 1

0

您可以通过请求将焦点移至 EditText 来执行此操作

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

于 2013-09-22T17:08:19.840 回答