-1

我正在制作网格布局并希望将其放入警报对话框中。但是当我尝试使用 show() 显示警报对话框时,但警报对话框没有显示。GridView相反,它在前面显示对话框,并且在GridView网格项目不可点击的背景中。这是编码:

public class GridViewActivity extends Activity {


Context context;
View layout;
static final String[] MOBILE_OS = new String[] { "timer", "about"};

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

    GridView gridView = (GridView)findViewById(R.id.gridView1);
    gridView.setAdapter(new ImageAdapter(this, MOBILE_OS));
    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {

            if(position == 0){
                Toast.makeText(GridViewActivity.this, "first icon is pressed", Toast.LENGTH_SHORT).show();
            }
            else if(position == 1){
                Toast.makeText(GridViewActivity.this, "second icon is pressed", Toast.LENGTH_SHORT).show();
            }
        }});
    Dialog();
}   
public void Dialog(){

    AlertDialog alertDialog;
    Context mContext = GridViewActivity.this;

    AlertDialog.Builder builder = new AlertDialog.Builder(GridViewActivity.this);
    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);

    View layout = inflater.inflate(R.layout.main,null);

    builder.setView(layout);
    alertDialog = builder.create();
 alertDialog.show();
  }

}

请告诉我如何解决这个问题。

4

1 回答 1

0

只需编辑为:

gridView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View v,
            int position, long id) {

        if(position == 0){
            Toast.makeText(GridViewActivity.this, "first icon is pressed", Toast.LENGTH_SHORT).show();
        }
        else if(position == 1){
            Toast.makeText(GridViewActivity.this, "second icon is pressed", Toast.LENGTH_SHORT).show();
        }
        ...............
        Dialog();  //  You Have called this method OUT-SIDE the click event
        .......................
    }});
于 2012-07-21T11:32:49.353 回答