2010-11-30 14 views
-1
<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     > 
    <LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="20px" 
      > 
     <Button android:id="@+id/expandButton" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text=" + " 
       android:focusable="false" 
       android:focusableInTouchMode="false" 
       android:clickable="false" 
       /> 
     <TextView android:id="@+id/jokeTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:textSize="16px" 
       android:singleLine="true" 
       /> 
    </LinearLayout> 
    <LinearLayout android:orientation="horizontal" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      > 
     <RadioGroup android:id="@+id/ratingRadioGroup" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 
      <RadioButton android:id="@+id/likeButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="60px" 
        android:text="Like" 
        /> 
      <RadioButton android:id="@+id/dislikeButton" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_marginLeft="60px" 
        android:text="Dislike" 
        /> 
     </RadioGroup> 
    </LinearLayout> 
</LinearLayout> 

I Klasse erweitert Ansicht (Linearlayout)Problem mit einem Knopf

import ... 

    public class JokeView extends LinearLayout{ 

     private Button m_vwExpandButton; 
     private RadioButton m_vwLikeButton; 
     private RadioButton m_vwDislikeButton; 
     private RadioGroup m_vwLikeGroup; 
     private TextView m_vwJokeText; 
     private Joke m_joke; 

     public static final String EXPAND = " + "; 
     public static final String COLLAPSE = " - "; 

     public JokeView(Context context, Joke joke) { 
      super(context); 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(
         Context.LAYOUT_INFLATER_SERVICE); 
      inflater.inflate(R.layout.joke_view, this, true); 

     m_vwExpandButton=(Button)findViewById(R.id.expandButton); 
      m_vwLikeButton=(RadioButton)findViewById(R.id.likeButton); 
      m_vwDislikeButton=(RadioButton)findViewById(R.id.dislikeButton); 
      m_vwLikeGroup=(RadioGroup)findViewById(R.id.ratingRadioGroup); 
      m_vwJokeText=(TextView)findViewById(R.id.jokeTextView); 

      setJoke(joke); 
       collapseJokeView(); 

       m_vwExpandButton.setOnClickListener(new OnClickListener(){ 
        @Override 
        public void onClick(View v){ 
         Log.e("tah", "onClick"); 
        if(m_vwExpandButton.getText().equals(JokeView.EXPAND)){ 
          expandJokeView();   
         } 
         else    
            if(m_vwExpandButton.getText().equals(JokeView.COLLAPSE)){ 
          collapseJokeView(); 
        } 
          } 
       }); 
      m_vwLikeGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){ 
        @Override 
        public void onCheckedChanged(RadioGroup rg, int i) { 
         if(i==1){ 
          m_joke.setRating(Joke.LIKE); 
         } 
         else if(i==2){ 
          m_joke.setRating(Joke.DISLIKE); 
         } 
        } 
       }); 
     } 

     public void setJoke(Joke joke) { 
      if(joke!=null){ 
       m_joke=joke; 
       m_vwJokeText.setText(joke.getJoke()); 
       switch(joke.getRating()){ 
       case Joke.LIKE: 
        m_vwLikeButton.setChecked(true); 
        break; 
       case Joke.DISLIKE: 
        m_vwDislikeButton.setChecked(true); 
        break; 
       case Joke.UNRATED: 
        m_vwLikeGroup.clearCheck(); 
        break; 
       } 
      } 
     } 

     private void expandJokeView() { 
      m_vwJokeText.setEllipsize(null); 
      m_vwJokeText.setSingleLine(false); 
      m_vwLikeGroup.setVisibility(View.VISIBLE); 
       m_vwExpandButton.setText(JokeView.COLLAPSE); 
      this.requestLayout(); 
     } 

     private void collapseJokeView() { 
      m_vwJokeText.setSingleLine(true); 
      m_vwJokeText.setEllipsize(TextUtils.TruncateAt.END); 
      m_vwLikeGroup.setVisibility(View.GONE); 
      m_vwExpandButton.setText(JokeView.EXPAND); 
      this.requestLayout(); 
     } 
    } 

Aber onClick passiert, wenn ich auf den Knopf klicken

+1

benötigen Sie spezifischere – jcollum

+0

Es ist nicht möglich, es sei denn Sie die onClick in der

+0

Was ist das Problem – Falmarri

Antwort

0

Die Frage ein bisschen vage. Aber ich vermute, da Sie keine onClick-Methode für das Klicken der Schaltfläche zur Verfügung gestellt haben, wird standardmäßig die einzige verwendet, die gefunden werden kann. Versuchen Sie folgendes:

android:onClick="expandButtonClicked"

bieten dann ein Verfahren für die Compiler zu finden.

0

Sicher, wenn Sie setzen m_vwExpandButton.setOnClickListener wird es anklickbar!

Beschreibung von den offiziellen Dokumentation:

setOnClickListener - einen Rückruf registriert aufgerufen werden, wenn diese Ansicht angeklickt wird. Wenn diese Ansicht nicht anklickbar ist, wird sie anklickbar.

+0

Aber Button wird nicht den Fokus erhalten. –

0

Ich mache dieses Labor, und ich habe dein Problem gelöst. Mine Und hier ist

package edu.calpoly.android.lab3; 

Import android.content.Context; importieren android.text.TextUtils.TruncateAt; importieren android.view.LayoutInflater; importieren android.view.View; importieren android.widget.Button; importieren android.widget.LinearLayout; importieren android.widget.RadioButton; importieren android.widget.RadioGroup; importieren android.widget.TextView;

public class JokeView erweitert Linearlayout implementiert RadioGroup.OnCheckedChangeListener { privaten Knopf m_vwExpandButton; Privater RadioButton m_vwLikeButton; Privater RadioButton m_vwDislikeButton; private RadioGroup m_vwLikeGroup; private TextView m_vwJokeText; private Witz m_joke;

public static final String EXPAND = " + "; 
public static final String COLLAPSE = " - "; 

/** 
* Basic Constructor that takes only takes in an application Context. 
* 
* @param context 
*   The application Context in which this view is being added. 
*    
* @param joke 
*   The Joke this view is responsible for displaying. 
*/ 
public JokeView(Context context, Joke joke) { 
    super(context); 

    // Inflate 
    LayoutInflater inflater = (LayoutInflater)context.getSystemService(
       Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.joke_view, this, true); 

    // Initialize 
    m_vwExpandButton = (Button)findViewById(R.id.expandButton); 
    m_vwLikeButton = (RadioButton)findViewById(R.id.likeButton); 
    m_vwDislikeButton = (RadioButton)findViewById(R.id.dislikeButton); 
    m_vwLikeGroup = (RadioGroup)findViewById(R.id.ratingRadioGroup); 
    m_vwJokeText = (TextView)findViewById(R.id.jokeTextview); 

    // Set joke 
    this.setJoke(joke); 

    // Set default state 
    JokeView.this.collapseJokeView(); 
    // Event 
    m_vwExpandButton.setOnClickListener(new OnClickListener() 
    { 
     @Override 
     public void onClick(View v) 
     { 
      if (m_vwExpandButton.getText().equals(JokeView.EXPAND)) 
       JokeView.this.expandJokeView(); 
      else if (m_vwExpandButton.getText().equals(JokeView.COLLAPSE)) 
       JokeView.this.collapseJokeView(); 
     } 
    }); 

    // Radio Group 
    m_vwLikeGroup.setOnCheckedChangeListener(JokeView.this); 
} 

/** 
* Mutator method for changing the Joke object this View displays. This View 
* will be updated to display the correct contents of the new Joke. 
* 
* @param joke 
*   The Joke object which this View will display. 
*/ 
public void setJoke(Joke joke) { 
    m_joke = joke; 
    m_vwJokeText.setText(joke.getJoke()); 

    if (joke.getRating() == Joke.LIKE) 
     m_vwLikeButton.setChecked(true); 
    if (joke.getRating() == Joke.DISLIKE) 
     m_vwDislikeButton.setChecked(true); 
} 

/** 
* This method encapsulates the logic necessary to update this view so that 
* it displays itself in its "Expanded" form: 
* - Shows the complete text of the joke. 
* - Brings the RadioGroup of rating Buttons back into view. 
*/ 
private void expandJokeView() { 
    m_vwJokeText.setEllipsize(null); 
    m_vwExpandButton.setText(JokeView.COLLAPSE); 
    m_vwLikeGroup.setVisibility(VISIBLE); 
    JokeView.this.requestLayout(); 
} 

/** 
* This method encapsulates the logic necessary to update this view so that 
* it displays itself in its "Collapsed" form: 
* - Shows only the first line of text of the joke. 
* - If the joke is longer than one line, it appends an ellipsis to the end. 
* - Removes the RadioGroup of rating Buttons from view. 
*/ 
private void collapseJokeView() { 
    m_vwJokeText.setEllipsize(TruncateAt.END); 
    m_vwExpandButton.setText(JokeView.EXPAND); 
    m_vwLikeGroup.setVisibility(GONE); 
    JokeView.this.requestLayout(); 
} 

@Override 
public void onCheckedChanged(RadioGroup group, int checkedId) 
{ 
    if (group.getCheckedRadioButtonId() == m_vwLikeButton.getId()) 
     m_joke.setRating(Joke.LIKE); 
    if (group.getCheckedRadioButtonId() == m_vwDislikeButton.getId()) 
     m_joke.setRating(Joke.DISLIKE);  
} 

}