Я занимаюсь этой лабораторией, и я решил твою проблему.
А вот мой
package edu.calpoly.android.lab3;
import android.content.Context;
import android.text.TextUtils.TruncateAt;
import android.view.LayoutInflater;
импорт android.view.View;
импорт android.widget.Button;
import android.widget.LinearLayout;
импорт android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
открытый класс JokeView расширяет LinearLayout, реализует RadioGroup.OnCheckedChangeListener
{
приватная кнопка m_vwExpandButton;
частное RadioButton m_vwLikeButton;
частное RadioButton m_vwDislikeButton;
частная радиогруппа m_vwLikeGroup;
частный TextView m_vwJokeText;
приватная шутка 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);
}
}