在TextView上ontouch和onclick不起作用。

3
请看下面的代码... 我在TextView上设置了onclick监听器和ontouch监听器,但它们都没有起作用...
问题似乎是什么?...
    AlertDialog.Builder builder;
    AlertDialog a;
    TextView text,txtNewUser ;
    ImageView image;
    View layout;
    Button ok;
    String pswrd;
    LayoutInflater inflater;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        final Context mContext = this;
        Dialog dialog = new Dialog(mContext);
        inflater = (LayoutInflater) mContext
                .getSystemService(LAYOUT_INFLATER_SERVICE);
        ok = (Button) findViewById(R.id.buttonOK);

        dialog.setContentView(R.layout.custom_dialog_private_space);
        dialog.setTitle("Password");
        dialog.setCancelable(true);
        layout = inflater.inflate(R.layout.custom_dialog_private_space,
                (ViewGroup) findViewById(R.id.layout));
        text = (TextView) dialog.findViewById(R.id.tvDesc);
        txtNewUser = (TextView) dialog.findViewById(R.id.tvNewUser);

        image = (ImageView) layout.findViewById(R.id.image);
        image.setImageResource(R.drawable.lock_symbol_android);

        builder = new AlertDialog.Builder(mContext);
        builder.setView(layout);

        a = builder.create();
        a.show();

        txtNewUser.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if(event.getAction() == MotionEvent.ACTION_UP)
                Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
                Log.v("AS", "Clicked");
    return true;

            }
        });

txtNewUser.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
        Log.v("AS", "Clicked");
    }
});
    }

这是对话框布局XML文件中TextView的代码:

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

<TextView android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/tvNewUser"
    android:text="New User?"
    android:layout_gravity="top|left"
    android:clickable="true"

    />

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
       />

    <TextView
        android:id="@+id/tvDesc"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="Enter password"
        android:textAppearance="?android:attr/textAppearanceLarge" 
        />



    <EditText
        android:id="@+id/etPassword"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="100"
        android:inputType="textPassword" >


    </EditText>

    <Button
        android:id="@+id/buttonOK"
        android:layout_width="100dp"
        android:layout_gravity="center"
        android:layout_height="fill_parent"
        android:text="OK" />

</LinearLayout>

这个TextView是某个对话框的一部分吗? - Shubhayu
你好,能否请您附上对话框的布局? - sravan
@kashifmehmood - 你试过我建议的吗? - user370305
我认为问题在于TextView被设置为对话框上的视图,当它尝试像findviewbyid这样查找TextView时,它会出现NPE...(这就是我认为的)。 - kashifmehmood
嗨,我发布了另一个答案,现在你可以查看那个答案,如果你想要完整的代码,请发送你的ID给我,我会发送代码。 - sravan
显示剩余11条评论
4个回答

3
  1. Instead of infalter use window class
  2. return false in on Touch listener
  3. set contenet view to your main activity

    package com.collabera.labs.sai;
    
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.Dialog;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.Window;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class HomePage extends Activity {
        AlertDialog.Builder builder;
        AlertDialog a;
        TextView text, txtNewUser;
        ImageView image;
        Button ok;
        String pswrd;
    
        public Window mWindow;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.custom_dialog_private_space);
            ok = (Button) findViewById(R.id.buttonOK);
            final Context mContext = this;
            Dialog dialog = new Dialog(HomePage.this);
    
            dialog.setContentView(R.layout.custom_dialog_private_space);
            dialog.setTitle("Password");
            dialog.setCancelable(true);
            dialog.show();
            mWindow = dialog.getWindow();
            Log.w(" dialog is " + mWindow.toString(), "-------");
            text = (TextView) mWindow.findViewById(R.id.tvDesc);
            txtNewUser = (TextView) mWindow.findViewById(R.id.tvNewUser);
            image = (ImageView) mWindow.findViewById(R.id.image);
            image.setImageResource(R.drawable.icon);
    
            txtNewUser.setOnTouchListener(new OnTouchListener() {
    
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (event.getAction() == MotionEvent.ACTION_UP)
                        Toast.makeText(mContext, "Touched", Toast.LENGTH_SHORT)
                                .show();
                    Log.v("AS", "Touched");
                    return false;
    
                }
            });
    
            txtNewUser.setOnClickListener(new View.OnClickListener() {
    
                @Override
                public void onClick(View v) {
                    Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
                    Log.v("AS", "Clicked");
                }
            });
        }
    }
    

对话框中既没有显示图像,也没有“确定”按钮,而且由于setContentView相同,对话框的前景与背景相同。 - kashifmehmood
你可以根据自己的需要设置内容视图,只需添加另一个XML文件即可。 - sravan
请使用android:height="wrap_content"代替fill parent。 - sravan
充气机到底出了什么问题...还是其他的问题...? - kashifmehmood

1

我认为...你也应该返回

return super.onTouchEvent(event);

从onTouch函数中...使用而不是true,因为true表示触摸事件已被消耗,其他操作不会调用此事件...在这种情况下使用onClick

方法 onTouchEvent 对象类型未定义 - kashifmehmood

0
在toast中,你在两种情况下都写了clicked,所以在onTouch监听器中将其更改为touch,并在onTouch监听器中返回false:
public class HomePage extends Activity {
    private TextView txtNewUser;
    private Context mContext;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mContext = getApplicationContext();
        txtNewUser = (TextView) findViewById(R.id.tvNewUser);

        txtNewUser.setOnTouchListener(new OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // TODO Auto-generated method stub
                if (event.getAction() == MotionEvent.ACTION_UP)
                    Toast.makeText(mContext, "Touched ", Toast.LENGTH_SHORT)
                            .show();
                Log.v("AS", "Touched");
                return false;

            }
        });

        txtNewUser.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(mContext, "Clicked", Toast.LENGTH_SHORT).show();
                Log.v("AS", "Clicked");
            }
        });

    }

这只是一条信息,不要紧,但是没有一个提示被执行。 - kashifmehmood
你好,现在试试这段代码吧。刚刚我试过了,如果你想的话,我可以把我的代码也发给你。 - sravan
这不是一个简单的TextView。这个TextView位于我正在创建的自定义对话框上...看这里... txtNewUser =(TextView)dialog.findViewById(R.id.tvNewUser); - kashifmehmood
请问您能否发送完整的项目? - sravan

0

这是我用来使ImageView可点击的方法:

public class Main extends Activity **implements OnClickListener** {

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


   ImageView  profile_btn=(ImageView) findViewById(R.id.x_Btn);
   x_btn.setOnClickListener(this) ;


    ImageView  food_btn=(ImageView) findViewById(R.id.y_Btn);

    ybtn.setOnClickListener(this); 
   }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
      switch(v.getId())
    {
    case R.id.x_Btn:
            //do some thing;break;
                  case R.id.y_Btn:
    //do some thing;break;
                  }
     }
}

我没有使用ImageView...我正在使用对话框上的TextView。 - kashifmehmood

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接