【问题标题】:How to change the text in a Popup Window TextView? [duplicate]如何更改弹出窗口 TextView 中的文本? [复制]
【发布时间】:2019-12-31 21:44:16
【问题描述】:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check_attendance);

    Intent intent = getIntent();



    final ArrayList<String> names = intent.getStringArrayListExtra("names");
    final ListView name_list = findViewById(R.id.name_list);
    final ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
            android.R.layout.simple_list_item_1, names);
    name_list.setAdapter(adapter);



    name_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Log.i("Name",names.get(i));

            LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupview = inflater.inflate(R.layout.popup_window, null);


            int width = LinearLayout.LayoutParams.WRAP_CONTENT;
            int height = LinearLayout.LayoutParams.WRAP_CONTENT;
            popupWindow  = new PopupWindow(popupview, width, height, true);
            popupWindow.showAtLocation(view, Gravity.CENTER, 0,0);



        }
    });
}

在上面的代码中,一切正常,除了我想将弹出窗口文本设置为列表视图中的单击元素。

我使用Log.i(),并且正在打印单击的元素,但是当我尝试将文本设置为此单击的元素时:

TextView sub_name = findViewById(R.id.subject_name);
sub_name.setText(names.get(i));

我收到以下错误:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.TextView.setText(java.lang.CharSequence)”

弹出窗口的XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="161dp"
        android:layout_marginTop="587dp"
        android:layout_marginEnd="162dp"
        android:layout_marginBottom="96dp"
        android:onClick="quit"
        android:text="Close"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/subject_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="176dp"
        android:layout_marginTop="258dp"
        android:layout_marginEnd="176dp"
        android:layout_marginBottom="455dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

    标签: java android


    【解决方案1】:

    在你给视图充气后放它

    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupview = inflater.inflate(R.layout.popup_window, null);
    int width = LinearLayout.LayoutParams.WRAP_CONTENT;
    int height = LinearLayout.LayoutParams.WRAP_CONTENT;
    popupWindow  = new PopupWindow(popupview, width, height, true);
    popupWindow.showAtLocation(view, Gravity.CENTER, 0,0);
    TextView sub_name = popupview.findViewById(R.id.subject_name);
    sub_name.setText(names.get(i));
    

    【讨论】:

    【解决方案2】:

    从 API 级别 1 起,您可以将内容放入 PopupWindow,这是一个新的临时窗口,您可以在其中放置将显示在当前活动窗口顶部的视图。 PopupWindow 可以显示在屏幕上的任何位置,方法是提供明确的位置或提供 PopupWindow 应锚定到的现有视图。

    PopupWindow 用于在指定位置显示浮动视图,但不插入或以其他方式修改现有视图层次结构。它是一个浮动容器,出现在当前活动之上。 PopupWindow 可以有自己的布局,可以通过 setContentView(View) 充气后设置。

    从 API 级别 18 起,您可以使用更新的 ViewOverlay 在视图之上绘制内容。 ViewOverlay 允许您将任意数量的 Drawable 对象添加到由父视图管理的私有层。只要它们的边界在父级的边界内,这些对象就会被绘制在相应视图的顶部。

    为了在我们的视图层次结构之上绘制内容,我们首先需要创建要显示的内容。下面的清单(文件 res/layout/popup.xml)构造了一组简单的视图,这些视图将成为我们的 PopupWindow 的内容

    Public class MainActivity extends Activity
        private PopupWindow popup;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            //inflate the popup content layout; we do not have access
            //to the parent view yet, so we pass null as the
            //container view parameter.
    
            View popupContent = getLayoutInflater().inflate(R.layout.popup, null);
            popup = new PopupWindow();
    
            //popup should wrap content view
            popup.setWindowLayoutMode(
                    WindowManager.LayoutParams.WRAP_CONTENT,
                    WindowManager.LayoutParams.WRAP_CONTENT);
            popup.setHeight(250);
            popup.setWidth(350);
    
            //set content and background
            popup.setContentView(popupContent);
            popup.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup_background));
    
            popupContent.findViewById(R.id.btnClose).setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    popup.dismiss();
                }
            });
    
            popup.setTouchInterceptor(this);
            popup.setFocusable(true);
            popup.setOutsideTouchable(true);
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            popup.dismiss();
        }
    
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            //Handle direct touch events passed to the PopupWindow
            return false;
        }
    
        public void onShowWindowClick(View v) {
            if (popup.isShowing()) {
                popup.dismiss();
            } else {
                //Show the PopupWindow anchored to the button we
                //pressed. It will be displayed below the button
                //if there's room, otherwise above.
                popup.showAsDropDown(v);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-26
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多