无法在android中设置editText的警报onclick事件

时间:2013-01-29 10:48:26

标签: android android-edittext alertdialog onclicklistener

我已经用一些EditTexts做了一个活动,我想在用户点击它时放一个警告对话框。所以我尝试了下面的代码,但它不能正常工作.can anybuddy请帮我解决问题,我的代码如下:

Main.java

package com.theappgeeks.kevingreenwealth;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class Calculator2 extends Activity {
Button btn1,btn2;
ImageButton btn3,btn4,btn5;;
SeekBar sk;
EditText et1,et2,et3,et4,et5,et6,et7,et8;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculator2);
        btn1=(Button)findViewById(R.id.back2);
        btn2=(Button)findViewById(R.id.Next2);
        btn3=(ImageButton)findViewById(R.id.imageButtonHome2);
        btn4=(ImageButton)findViewById(R.id.imageButtonTraining2);
        btn5=(ImageButton)findViewById(R.id.imageButtonContactUs2);
        sk=(SeekBar)findViewById(R.id.seekBar1);
        sk=(SeekBar)findViewById(R.id.seekBar1);
        sk.setClickable(false);
        sk.setEnabled(false);
        sk.setProgress(sk.getMax()/4);

        et1=(EditText)findViewById(R.id.editTextPropertyCalc1);
        et2=(EditText)findViewById(R.id.editTextPropertyCalc2);
        et3=(EditText)findViewById(R.id.editTextPropertyCalc9);
        et4=(EditText)findViewById(R.id.editTextPropertyCalc4);
        et5=(EditText)findViewById(R.id.editTextPropertyCalc5);
        et6=(EditText)findViewById(R.id.editTextPropertyCalc6);
        et7=(EditText)findViewById(R.id.editTextPropertyCalc7);
        et8=(EditText)findViewById(R.id.editTextPropertyCalc8);

        final AlertDialog a1 = new AlertDialog.Builder(this).create();

        et1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                a1.setTitle("Auto-generated field");
                a1.setMessage("This field is calculated automatically");
                a1.setButton("Ok", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int which) {
                      // TODO Add your code for the button here.

                      // Toast.makeText(getApplicationContext(), "Basic Calculator", 1).show();
                   }
                });
                a1.show();
            }
        });

        btn1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i1 =new Intent(Calculator2.this,Calculator_1Activity.class);
                startActivity(i1);
            }
        });
        btn2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent i2 =new Intent(Calculator2.this,Calculator_3Activity.class);
                startActivity(i2);

            }
        });

  btn3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i3=new Intent(Calculator2.this,MainMenuActivity.class);
            startActivity(i3);
            }
        });

 btn4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            Intent i4=new Intent(Calculator2.this,TrainingActivity.class);
            startActivity(i4);
            }
        });

 btn5.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
        Intent i5=new Intent(Calculator2.this,ContactUsActivity.class);
        startActivity(i5);
        }
    });
    }
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        ((InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(0, 0);
        return false;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_calculator2, menu);
        return true;
    }
}

main.xml中

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

    <RelativeLayout
        android:id="@+id/relativeLayout2"
        android:layout_width="fill_parent"
        android:layout_height="80dp"
        android:padding="3dp" >

        <SeekBar
            android:id="@+id/seekBar1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true" />

        <Button
            android:id="@+id/Next2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_alignRight="@+id/seekBar1"
            android:text="Next" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/Next2"
            android:layout_alignBottom="@+id/Next2"
            android:layout_centerHorizontal="true"
            android:text="Mortage Cover"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textStyle="bold" />

        <Button
            android:id="@+id/back2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/seekBar1"
            android:layout_alignParentTop="true"
            android:text="Back" />

    </RelativeLayout>

        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"  android:layout_weight="0.23" android:fillViewport="true">

            <RelativeLayout
                android:id="@+id/relativeLayout2"
                android:layout_width="fill_parent"
                android:layout_height="509dp"
                android:background="#fffff3"
                android:padding="10dp" >

                <EditText
                    android:id="@+id/editTextPropertyCalc1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc5"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="15dp"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:imeOptions="actionNext"
                    android:inputType="numberDecimal" >

                </EditText>

                <EditText
                    android:id="@+id/editTextPropertyCalc2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc1"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/editTextPropertyCalc1"
                    android:imeOptions="actionDone"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:inputType="numberDecimal" />

                <EditText
                    android:id="@+id/editTextPropertyCalc3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc1"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/editTextPropertyCalc2"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:inputType="numberDecimal" />

                <TextView
                    android:id="@+id/textView2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/editTextPropertyCalc3"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentRight="true"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:layout_below="@+id/editTextPropertyCalc3"
                    android:textAppearance="?android:attr/textAppearanceMediumInverse"
                    android:typeface="serif" />

                <EditText
                    android:id="@+id/editTextPropertyCalc4"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc3"
                    android:layout_alignRight="@+id/textView2"
                    android:layout_below="@+id/textView2"
                    android:imeOptions="actionNext"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:inputType="numberDecimal" />

                <EditText
                    android:id="@+id/editTextPropertyCalc5"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignRight="@+id/textView2"
                    android:layout_below="@+id/editTextPropertyCalc4"
                    android:layout_toRightOf="@+id/imageButtonCalcTip1"
                    android:imeOptions="actionNext"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:inputType="numberDecimal" />

                <EditText
                    android:id="@+id/editTextPropertyCalc6"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc4"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/editTextPropertyCalc5"
                    android:imeOptions="actionNext"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:inputType="numberDecimal" />

                <EditText
                    android:id="@+id/editTextPropertyCalc7"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc5"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/editTextPropertyCalc6"
                    android:imeOptions="actionDone"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:inputType="numberDecimal" />

                <EditText
                    android:id="@+id/editTextPropertyCalc8"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc6"
                    android:layout_alignParentRight="true"
                    android:layout_below="@+id/editTextPropertyCalc7"
                    android:enabled="false"
                    android:focusable="false"
                    android:focusableInTouchMode="false"
                    android:inputType="numberDecimal" />

                <TextView
                    android:id="@+id/TextView01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/editTextPropertyCalc1"
                    android:layout_alignBottom="@+id/editTextPropertyCalc1"
                    android:layout_alignLeft="@+id/textView2"
                    android:layout_marginTop="15dp"
                    android:text="Price"
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:textSize="20dp"
                    android:typeface="serif" />

                <TextView
                    android:id="@+id/TextView03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/editTextPropertyCalc3"
                    android:layout_alignBottom="@+id/editTextPropertyCalc3"
                    android:layout_alignLeft="@+id/textView2"
                    android:text="Mortage Amount"
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:typeface="serif" />

                <TextView
                    android:id="@+id/TextView02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/editTextPropertyCalc4"
                    android:layout_alignBottom="@+id/editTextPropertyCalc4"
                    android:layout_alignParentLeft="true"
                    android:text="Mortage Interest Rate% "
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:typeface="serif" />

                <TextView
                    android:id="@+id/TextView04"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/editTextPropertyCalc5"
                    android:layout_alignBottom="@+id/editTextPropertyCalc5"
                    android:layout_alignParentLeft="true"
                    android:layout_alignRight="@+id/TextView02"
                    android:text="Monthly Interest"
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:typeface="serif" />

                <TextView
                    android:id="@+id/TextView05"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/editTextPropertyCalc6"
                    android:layout_alignBottom="@+id/editTextPropertyCalc6"
                    android:layout_alignParentLeft="true"
                    android:text="Lender&apos;s Rent Cover %"
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:typeface="serif" />

                <TextView
                    android:id="@+id/TextView06"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/editTextPropertyCalc7"
                    android:layout_alignBottom="@+id/editTextPropertyCalc7"
                    android:layout_alignParentLeft="true"
                    android:layout_alignRight="@+id/textView3"
                    android:layout_marginBottom="20dp"
                    android:text="Lender&apos;s Monthly Rent Required"
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:typeface="serif" />

                <TextView
                    android:id="@+id/TextView07"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/editTextPropertyCalc8"
                    android:layout_alignLeft="@+id/TextView06"
                    android:layout_alignRight="@+id/TextView04"
                    android:layout_alignTop="@+id/editTextPropertyCalc8"
                    android:layout_marginTop="15dp"
                    android:text="Montlhly Rent Received"
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:typeface="serif" />
                    />

                <EditText
                    android:id="@+id/editTextPropertyCalc9"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignLeft="@+id/editTextPropertyCalc2"
                    android:layout_below="@+id/editTextPropertyCalc2"
                    android:ems="10"
                    android:enabled="false"
                    android:focusable="false"

                    android:focusableInTouchMode="true"
                    android:inputType="numberDecimal" >

                    <requestFocus />
                </EditText>

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/editTextPropertyCalc2"
                    android:layout_alignBottom="@+id/editTextPropertyCalc2"
                    android:layout_alignLeft="@+id/TextView03"
                    android:gravity="center"
                    android:text="Mortage Loan to value %"
                    android:textAppearance="?android:attr/textAppearanceSmallInverse"
                    android:typeface="serif" />

                <ImageButton
                    android:id="@+id/imageButtonCalcTip1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/editTextPropertyCalc4"
                    android:layout_toRightOf="@+id/TextView06"
                    android:background="#0000"
                    android:padding="10dp"
                    android:scaleType="fitCenter"
                    android:src="@android:drawable/ic_dialog_info" />

                <ImageButton
                    android:id="@+id/ImageButton02"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/editTextPropertyCalc7"
                    android:layout_toLeftOf="@+id/editTextPropertyCalc6"
                    android:background="#0000"
                    android:padding="10dp"
                    android:scaleType="fitCenter"
                    android:src="@android:drawable/ic_dialog_info" />

                <ImageButton
                    android:id="@+id/ImageButton01"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_above="@+id/ImageButton02"
                    android:layout_toLeftOf="@+id/editTextPropertyCalc5"
                    android:background="#0000"
                    android:padding="10dp"
                    android:scaleType="fitCenter"
                    android:src="@android:drawable/ic_dialog_info" />
                <ImageButton 
                    android:id="@+id/ImageButton03"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/ImageButton02"
                    android:padding="10dp"
                       android:background="#0000"
                    android:scaleType="fitCenter"
                    android:layout_toLeftOf="@+id/editTextPropertyCalc8"
                    android:src="@android:drawable/ic_dialog_info"/>

            </RelativeLayout>

</ScrollView>

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
             android:layout_weight="0.60"
            android:background="@drawable/gradient_grey" >

            <LinearLayout
                android:layout_width="81dp"
                android:layout_height="fill_parent"
                android:layout_marginRight="5dp"
                android:orientation="vertical" >

            <ImageButton
                android:id="@+id/imageButtonHome2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginRight="8dp"
                android:src="@drawable/g3570" android:layout_weight="0.6" android:background="#0000" android:scaleType="fitCenter" android:layout_marginTop="5dp"/>

            <TextView                 android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                 android:layout_weight="1"
                 android:text="Home"
                android:gravity="center" android:textAppearance="?android:attr/textAppearanceMediumInverse" android:textColor="#fff" android:textStyle="bold"/>

            </LinearLayout> <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" android:orientation="vertical">

            <ImageButton
                android:id="@+id/imageButtonTraining2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/g4404" android:layout_weight="0.6" android:scaleType="fitCenter" android:background="#0000" android:layout_marginTop="5dp"/>
                <TextView                
                     android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                 android:layout_weight="1"
                 android:text="Training"
                android:gravity="center" android:textAppearance="?android:attr/textAppearanceMediumInverse" android:textColor="#fff" android:textStyle="bold"/>

            </LinearLayout> 

            <LinearLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1" android:orientation="vertical">

            <ImageButton
                android:id="@+id/imageButtonContactUs2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/icon_email" android:layout_weight="0.6" android:scaleType="fitCenter" android:background="#0000" android:layout_marginTop="5dp"/>

               <TextView                 android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                 android:layout_weight="1"
                 android:text="Contact Us"
                android:gravity="center" android:textAppearance="?android:attr/textAppearanceMediumInverse" android:textColor="#fff" android:textStyle="bold"/>

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>

logcat的

01-29 10:57:01.381: D/AndroidRuntime(297): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
01-29 10:57:01.381: D/AndroidRuntime(297): CheckJNI is ON
01-29 10:57:01.521: D/AndroidRuntime(297): --- registering native functions ---
01-29 10:57:02.072: I/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.theappgeeks.kevingreenwealth/.KevinGreenWealthActivity }
01-29 10:57:02.082: D/AndroidRuntime(297): Shutting down VM
01-29 10:57:02.082: D/dalvikvm(297): Debugger has detached; object registry had 1 entries
01-29 10:57:02.102: I/AndroidRuntime(297): NOTE: attach of thread 'Binder Thread #3' failed

1 个答案:

答案 0 :(得分:1)

@+id/editTextPropertyCalc1更改的XML定义中:

android:enabled="false"

android:enabled="true"