2012-04-16 47 views
3

一個TextView看看下面的代碼... 我設置上的TextView和ontouch監聽器,但他們都不是工作的onclick聽衆......ontouch和的onclick上不工作

似乎什麼成爲問題> ....?

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"); 
    } 
}); 
    } 

這裏的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> 
+0

您能用完整的XML補充的嗎? – 2012-04-16 10:20:07

+0

你有沒有使用自定義對話框?如果沒有那麼txtNewUser =(TextView)dialog.findViewById(R.id.tvNewUser); 從上面的行中刪除對話框。 – 2012-04-16 10:21:59

+0

這個textview是一些對話框的一部分嗎? – Shubhayu 2012-04-16 10:23:17

回答

2
  1. 而不是使用過窗類
  2. 返回false在觸碰聆聽者
  3. 集contenet以您的主要活動

    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"); 
          } 
         }); 
        } 
    } 
    
+0

既不是圖像表現也不確定按鈕,再加上對話的前景因爲setContentView與背景相同,所以與 – kashifmehmood 2012-04-16 11:50:15

+0

相同所以你可以設置內容查看你想如何添加另一個XML文件。 – sravan 2012-04-16 11:53:55

+0

按鈕和圖像以及編輯文字怎麼樣?????????? – kashifmehmood 2012-04-16 11:55:12

1

我想的XML代碼..你也應該返回

return super.onTouchEvent(event); 

從onTouch功能.. ,而不是true,因爲true意味着該事件被觸摸消耗,並且其他操作不會針對此事件調用。在這種情況下,onClick

+0

該方法onTouchEvent是未定義的類型對象 – kashifmehmood 2012-04-16 10:41:35

+0

仍然沒有.... :( – kashifmehmood 2012-04-16 10:51:57

+0

沒有你的意思..沒有人被稱爲? – 5hssba 2012-04-16 11:04:21

0

在敬酒你寫單擊了這樣改變onTouch聽衆接觸,並在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"); 
      } 
     }); 

    } 
+0

它只是一個消息,並沒有關係,但沒有執行任何敬酒 – kashifmehmood 2012-04-16 10:43:44

+0

你好現在試試這個代碼,只是現在我試過了,如果你想我也會發送我的代碼。 (TextView)dialog.findViewById(R.id.tvNewUser); – sravan 2012-04-16 10:45:00

+0

它不是一個簡單的textview。textview位於一個自定義對話框,我正在創建... c這個... txtNewUser =(TextView)dialog.findViewById項目? – kashifmehmood 2012-04-16 10:47:04

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; 
        } 
    } 
} 
+0

我不使用的ImageView ...我使用的TextView出現在一個對話框 – kashifmehmood 2012-04-16 11:01:32