YXTS122 2016-11-17 01:26 采纳率: 100%
浏览 986
已采纳

怎么再按一次"选择"按钮,就报说已停止?

MainActivity.java

 package com.example.ch02_dialogdemo;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity implements OnClickListener{
      private View myLoginView=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LayoutInflater layoutInflater=LayoutInflater.from(this );
        myLoginView=layoutInflater.inflate(R.layout.login,null);
        Button button=(Button)findViewById(R.id.bt1);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {
       switch (v.getId())
       {
       case R.id.bt1:


AlertDialog alertDialog=new AlertDialog.Builder(this ).create();
alertDialog.setTitle("用户登录");
alertDialog.setView(myLoginView);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"确定",listener);
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"取消",liss);
alertDialog.show();
       }
    }

    DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface  dialog,int which)
        {

        }
    };

    DialogInterface.OnClickListener liss=new DialogInterface.OnClickListener()
    {
        public void onClick(DialogInterface  dialog,int which)
        {
        }
    };


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

activity_main.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="com.example.ch02_dialogdemo.MainActivity" >

    <Button 
        android:id="@+id/bt1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/xuan"
         android:textSize="20sp"/>

</RelativeLayout>

login.xml

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


    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <EditText
            android:inputType="text"
           android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
     </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center">
        <TextView
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
        <EditText
            android:inputType="text"
           android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1" />
     </LinearLayout>


</LinearLayout>

strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources>

    <string name="app_name">DialogDemo</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="xuan">选择</string>
</resources>

图片说明

图片说明

图片说明

图片说明

  • 写回答

4条回答 默认 最新

  • holamP 2016-11-17 02:12
    关注

    1。dialog创建之前做个判断

    switch (v.getId())
    {
    case R.id.bt1:

                if(mAlertDialog==null){
                mAlertDialog = new AlertDialog.Builder(this ).create();
                }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档
  • ¥60 全一数分解素因子和素数循环节位数
  • ¥15 ffmpeg如何安装到虚拟环境
  • ¥188 寻找能做王者评分提取的
  • ¥15 matlab用simulink求解一个二阶微分方程,要求截图
  • ¥30 乘子法解约束最优化问题的matlab代码文件,最好有matlab代码文件
  • ¥15 写论文,需要数据支撑