程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 編程-怎麼再按一次選擇按鈕,就報說已停止?

編程-怎麼再按一次選擇按鈕,就報說已停止?

編輯:編程解疑
怎麼再按一次"選擇"按鈕,就報說已停止?

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>

圖片說明

圖片說明

圖片說明

圖片說明

最佳回答:


1。dialog創建之前做個判斷

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

            if(mAlertDialog==null){
            mAlertDialog = new AlertDialog.Builder(this ).create();
            }
YXTS122
YXTS122
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved