gpt4 book ai didi

java - 当我从列表中选择一个项目时,如何打开带有按钮的弹出窗口?-在 Android 应用程序中

转载 作者:行者123 更新时间:2023-12-01 13:17:23 24 4
gpt4 key购买 nike

我的应用程序有问题。我有一个项目列表,当我选择一个项目时,我想展示一个弹出窗口,其中包含一些文本和一个按钮,该按钮将带我进入另一项 Activity 。使用到目前为止我拥有的代码,我有列表,但不起作用。我需要将弹出窗口和过渡添加到另一个 Activity 。有人可以帮助我解决我的问题吗? 这就是我到目前为止所拥有的..:

主要 Activity :

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ListView;

public class Lesson extends Activity {

public static int [] prgmImages= {R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,
R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,};
public static String[] prgmNameList = {"Ex1","Ex2","Ex3","Ex4","Ex5","Ex6","Ex7","Ex8","Ex9","Ex10","Ex11","Ex12",};
public static String[] prgmDescription = {"asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds",
"asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds",
"asdafasdsad","asdsafasfas","aaaaaa","lkjhggfdds"};

Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lesson);

context = this;

ListView lv = (ListView) findViewById(R.id.listView1);
lv.setAdapter(new CustomAdapter(this, prgmNameList, prgmImages, prgmDescription));
}
}

CustomAdapter.java:

package com.example.test;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {

String [] description;
String [] result;
Context context;
int [] imageId;
private static LayoutInflater inflater=null;

public CustomAdapter(Lesson lesson, String[] prgmNameList, int[] prgmImages, String[] prgmDescription){
// TODO Auto-generated constructor stub
result=prgmNameList;
description=prgmDescription;
context=lesson;
imageId=prgmImages;
inflater = ( LayoutInflater )context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public class Holder
{
TextView tv;
TextView tv2;
ImageView img;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.list_field, null);
holder.tv2=(TextView) rowView.findViewById(R.id.textView2);
holder.tv=(TextView) rowView.findViewById(R.id.textView1);
holder.img=(ImageView) rowView.findViewById(R.id.imageView1);

holder.tv2.setText(description[position]);
holder.tv.setText(result[position]);
holder.img.setImageResource(imageId[position]);

rowView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(context, "You Clicked "+result[position], 2000).show();

}
});
return rowView;
}
}

activity_lessons.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Lesson" >

<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>

</RelativeLayout>

列表字段:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<TextView
android:id="@+id/textView1"
android:layout_width="200dp"
android:layout_height="30dp"
android:layout_alignTop="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />

<TextView
android:id="@+id/textView2"
android:layout_width="200dp"
android:layout_height="18dp"
android:layout_below="@+id/textView1"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />

<ImageView
android:id="@+id/imageView1"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />

</RelativeLayout>

最佳答案

您可以像这样打开项目点击警报:

rowView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(context, "You Clicked "+result[position], 2000).show();

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");

builder.setMessage("Message");
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener()
{

public void onClick(DialogInterface dialog, int id)
{
dialog.cancel();
Intent intent = new Intent(context, Activity2.class);
context.startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();

}
});

关于java - 当我从列表中选择一个项目时,如何打开带有按钮的弹出窗口?-在 Android 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22358556/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com