java - 将自定义弹出窗口/ View 添加到 android 中的 Activity

标签 java android user-interface

我刚刚开始 Android 开发,但仍然不知道如何做到这一点。我有一个 ListView,当用户点击其中一个项目时,我不想显示有关它的一些信息。 像这样的弹出窗口: enter image description here

这就是它应该的样子,我已经有了这样的 XML:

<RelativeLayout 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"
android:background="#b5000000">

<RelativeLayout
    android:layout_width="340dp"
    android:layout_height="400dp"
    android:columnCount="20"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:padding="10dp"
    android:background="@drawable/menubutton">

    <Button
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:text="X"
        android:id="@+id/infraccionesPopUpCerrar"
        android:layout_gravity="right|top"
        android:layout_row="0"
        android:layout_column="19"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:id="@+id/imageView8"
        android:layout_row="2"
        android:layout_column="10"
        android:layout_below="@+id/infraccionesPopUpCerrar"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:text="Aqui se detallaran las causas de la infraccion"
        android:id="@+id/infraccionesPopUpCausa"
        android:layout_column="0"
        android:layout_row="3"
        android:layout_columnSpan="20"
        android:layout_below="@+id/imageView8"
        android:layout_alignParentStart="true"
        android:layout_marginTop="43dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Small Text"
        android:id="@+id/infraccionesPopUpFecha"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="PAGADA"
        android:id="@+id/infraccionesPopUpSituacion"
        android:layout_below="@+id/infraccionesPopUpCausa"
        android:layout_alignStart="@+id/infraccionesPopUpCausa" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:text="Folio: 00000000"
        android:id="@+id/infraccionesPopUpFolio"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/infraccionesPopUpSituacion" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Estacionamiento prohibido"
        android:id="@+id/infraccionesPopUpTitulo"
        android:layout_below="@+id/imageView8"
        android:layout_centerHorizontal="true"
        android:textColor="#74cb51" />

</RelativeLayout>

如何实例化此弹出窗口,以便更改其中 TextView 的内容?显示弹出窗口的信息取决于他点击的项目

最佳答案

public class Check extends AppCompatActivity {
ListView listView;
String[] days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
ArrayAdapter<CharSequence> adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register_check);

    listView = (ListView) findViewById(R.id.listView);
    //custom_list_row contains textview only
    adapter = new ArrayAdapter<CharSequence>(this, R.layout.custom_list_row, days);
    listView.setAdapter(adapter);


    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //pass the view
            showPopUp(view);
        }
    });
}

public void showPopUp(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater layoutInflater = getLayoutInflater();

    //this is custom dialog 
    //custom_popup_dialog contains textview only
    View customView = layoutInflater.inflate(R.layout.custom_popup_dialog, null);
    // reference the textview of custom_popup_dialog
    TextView tv = (TextView) customView.findViewById(R.id.tvpopup);


    //this textview is from the adapter
    TextView text = (TextView) view.findViewById(R.id.textView);
    // get the text of the view clicked
    String day = text.getText().toString();
    //set the text to the view of custom_popop_dialog.
    tv.setText(day);

    builder.setView(customView);
    builder.create();
    builder.show();

}

}

关于java - 将自定义弹出窗口/ View 添加到 android 中的 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37494648/

相关文章:

java - 从 Servlet(或过滤器)访问 Tomcat 内部

java - Java布局中的元素都在同一位置

java - 在 JSTL 循环中调用带参数的方法

windows - Windows 上的 Mac 风格菜单,系统范围

java - 计算正多边形面积问题

java - Android 从 app.Fragment 导航到 PreferenceFragment

Android 联系人选择器获取姓名 + 号码 + 电子邮件

android - Firebase Android NotificationCompat.PRIORITY_HIGH 在应用程序处于后台时无法正常工作

android - 是否有适用于 Android 的第 3 方 UI 库?

python 用户界面卡住