用户为安卓网格视图中的每个图像输入一个名称



我一直在寻找这个问题的答案多少天了,但我找不到正确的答案,所以我尝试创建自己的问题。

我创建了一个包含图像的网格视图,每个图像都必须具有特定名称。单击图像时,将提示一个对话框,并要求为单击的图像指定的图像名称。

有什么可能的事情可以做到这一点?请帮忙。我已经包含了来自网格视图的网络代码:

网格.java

public class Grid extends Activity{
    public static Integer[] homeIC = {
            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
    };
    // I've used blank strings so that I could change it to the name desired by the user
    public static String[] menuName = {
            " ", " ", " ", " ", " ", " ", " ", " "
    };
    GridView gridV;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid);
        gridV = (GridView) findViewById(R.id.gridview);
        gridV.setAdapter(new ImageAdapter(this, menuName, homeIC));
    }
public class ImageAdapter extends BaseAdapter
{
    private Context context;
    String[] homeText;
    Integer[] imageID;
    private LayoutInflater inflater=null;
    public ImageAdapter (Grid mainactivity, String[] name, Integer[] image)
    {
        context = mainactivity;
        homeText = name;
        imageID = image;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    //--returns the number of images
    public int getCount()
    {
        return homeIC.length;
    }
    //returns the ID of an item--
    public Object getItem(int position)
    {
        return position;
    }
    //returns the ID of an item
    public long getItemId(int position)
    {
        return position;
    }
    public class Holder
    {
        TextView tv;
        ImageView img;
    }
    public View getView(final int position, View convertView, ViewGroup parent)
    {       
        final Holder holder=new Holder();
        View gridV;
             gridV = inflater.inflate(R.layout.grid_content, null);
             holder.tv=(TextView) gridV.findViewById(R.id.homeText);
             holder.img=(ImageView) gridV.findViewById(R.id.homeImage);
         holder.img.setImageResource(imageID[position]);
         holder.tv.setText(homeText[position]);
         gridV.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                //show dialog box and ask for a name for an image
            }
        });
        return gridV;
    }
    }
}

activity_grid.xml

<LinearLayout 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:background="#bbbbbb"
        android:layout_weight="1"
        android:orientation="vertical"
        tools:context="${relativePackage}.${activityClass}" >
     <GridView
            android:id="@+id/gridview"
            android:layout_width="fill_parent"
            android:layout_height="0dip"
            android:layout_marginBottom="10dp"
            android:layout_marginLeft="30dp"
            android:layout_marginRight="30dp"
            android:layout_marginTop="5dp"
            android:layout_weight="0.05"
            android:columnWidth="80dp"
            android:gravity="center"
            android:numColumns="auto_fit"
            android:stretchMode="columnWidth" >
        </GridView>
    </LinearLayout>

grid_content.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
    android:id="@+id/homeImage"
    android:layout_width="80dp"
    android:layout_height="80dp"
    android:layout_gravity="center"
    android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
    android:id="@+id/homeText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="5dp"
    android:gravity="center"
    android:textSize="15sp" >
</TextView>

这是对话框的XML文件,用户将在添加图像名称时使用该文件

add_name.xml

    <?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/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name: "
        android:textAppearance="?android:attr/textAppearanceMedium" />
    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/TextView01"
        android:ems="10" />

    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="66dp"
        android:text="ADD" />
    <Button
        android:id="@+id/cancel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/ok"
        android:layout_alignBottom="@+id/ok"
        android:layout_toRightOf="@+id/ok"
        android:text="CANCEL" />
</RelativeLayout>

感谢您的任何回复。这将是一个很大的帮助。

onClick里面像这样使用它

AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Enter image name");
alert.setMessage("Please enter the image name below");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText();
        //Get the text view and set its value
    }
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // Canceled.
    }
});
alert.show();

相关内容

  • 没有找到相关文章

最新更新