自定义对话框,显示图像,文字和的setTitle(Custom Dialog Boxes to sh

2019-09-01 20:27发布

我在我的节目一个小故障,我一直试图弄清楚它是什么我失踪。 在我的java文件,我已经在那里我就在GridView项目setOnItemsClickListener图像的GridView控件。 我有2个XML文件,一个是的.java和其他被设置为内容查看上点击任何的GridView项弹出窗口。 我想它,每当用户点击gridview的项目,自定义对话框弹出,显示图像点击,再加上图像的简要说明。

到目前为止,对话框弹出相当好的,但没有详细问此对话框通过。

这是我的activity_first.xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >

<GridView
    android:id="@+id/gridView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:numColumns="3" 
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:columnWidth="90dp"
    android:gravity="center" >

</GridView>

</LinearLayout>

这是我activity_custom_dialog.xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background">
    <TableLayout    
        android:id="@+id/tableLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <!-- 2 columns -->
        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip" >

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp" />
            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </TableRow>

        <!-- Button span 2 column -->
        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5dip" >

    <Button
                android:id="@+id/button1"
                android:text="Ok" />
        </TableRow>
    </TableLayout>

</LinearLayout>

最后,这是我的Image.Java文件看起来像:

package com.example.custom;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


public class ImageActivity extends Activity {
    // Images to display
    Integer[] imageIDs = {
            R.drawable.firefighter,     R.drawable.hydrant,
            R.drawable.fire,            R.drawable.hose,
            R.drawable.truck,           R.drawable.ladder,
            R.drawable.safety_clothing, R.drawable.gloves,
    };
    /**
     * onCreate
     *
     * Called when the activity is first created. 
     * This is where you should do all of your normal static set up: create views, bind data to lists, etc. 
     * This method also provides you with a Bundle containing the activity's previously frozen state, if there was one.
     * 
     * Always followed by onStart().
     *
     * @param savedInstanceState Bundle
     */

    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        setContentView (R.layout.activity_first);
        setTitleFromActivityLabel (R.id.title_text);

        GridView gridview = (GridView) findViewById(R.id.gridView1);
           gridview.setAdapter(new ImageAdapter(this));

           gridview.setOnItemClickListener(new OnItemClickListener()
           {
               public void onItemClick(AdapterView<?> parent, View v, int position, long id)
               {
                    final Dialog dialog = new Dialog(context);
                    dialog.setContentView(R.layout.activity_custom_dialog);

                   switch(v.getId())
                   {
                   case R.drawable.firefighter:
                        dialog.setTitle("FireFighter");                    
                        TextView text = (TextView) dialog.findViewById(R.id.textView1);
                        text.setText("To insert text...");
                        ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);

                        image.setImageResource(R.drawable.firefighter);
                   break;

                   case R.drawable.hydrant:
                        dialog.setTitle("Hydrant");                    
                        TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
                        text1.setText("To insert text...");
                        ImageView image1 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image1.setImageResource(R.drawable.hydrant);
                   break;
                   case R.drawable.fire:
                        dialog.setTitle("Fire");                       
                        TextView text2 = (TextView) dialog.findViewById(R.id.textView1);
                        text2.setText("To insert text...");
                        ImageView image2 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image2.setImageResource(R.drawable.fire);
                   break;
                   case R.drawable.hose:
                        dialog.setTitle("Hose");                       
                        TextView text3 = (TextView) dialog.findViewById(R.id.textView1);
                        text3.setText("To insert text...");
                        ImageView image3 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image3.setImageResource(R.drawable.hose);
                   break;
                   case R.drawable.truck:
                        dialog.setTitle("Truck");                      
                        TextView text4 = (TextView) dialog.findViewById(R.id.textView1);
                        text4.setText("To insert text...");
                        ImageView image4 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image4.setImageResource(R.drawable.truck);
                   break;
                   case R.drawable.ladder:
                        dialog.setTitle("Ladder");                     
                        TextView text5 = (TextView) dialog.findViewById(R.id.textView1);
                        text5.setText("To insert text...");
                        ImageView image5 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image5.setImageResource(R.drawable.ladder);
                   break;
                   case R.drawable.gloves:
                        dialog.setTitle("Gloves");                     
                        TextView text6 = (TextView) dialog.findViewById(R.id.textView1);
                        text6.setText("To insert text...");
                        ImageView image6 = (ImageView) dialog.findViewById(R.id.imageView1);

                        image6.setImageResource(R.drawable.gloves);
                   break;
                   }

                    // set the custom dialog components - text, image and button

                    Button dialogButton = (Button) dialog.findViewById(R.id.button1);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new OnClickListener() {

                        public void onClick(View v) {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();
           /*                      Toast.makeText(getBaseContext(), "Pic "+(position+1)+
                                  " selected", Toast.LENGTH_SHORT).show();
*/             }
           });
    }

    public class ImageAdapter extends BaseAdapter
    {
        private Context context;

        public ImageAdapter(Context c)
        {
            context = c;
        }


       // Returns the number of images
        public int getCount(){
         return imageIDs.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;
        }

        // Returns an ImageView View
        public View getView(int position, View convertView, ViewGroup parent){
            ImageView imageView;
            if(convertView == null){
                imageView = new ImageView(context);
                imageView.setLayoutParams(new GridView.LayoutParams(100,100));
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setPadding(5, 5, 5, 5);
            } 
            else{
                imageView = (ImageView) convertView;
            }

            imageView.setImageResource(imageIDs[position]);
            return imageView;
        }

    }

} // end class

我真的很感激它,我应该得到的指导尽快就如何解决这个律毛刺我的律程序员的眼睛看不到。 :-)

提前致谢。

文章来源: Custom Dialog Boxes to show image, text and setTitle