Problem with a button
<?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 have class extends View(LinearLayout)
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();
}
}
But onClick happens when I click on button
The question is a bit vague. But I suspect since you haven't provided an onClick method for the button click to call, it's defaulting to the only one it can find. Try this:
android:onClick="expandButtonClicked"
Then provide a method for the compiler to find.
Sure, when you set m_vwExpandButton.setOnClickListener it becomes clickable!
Description from official documentation:
setOnClickListener - Register a callback to be invoked when this view is clicked. If this view is not clickable, it becomes clickable.
I'm doing this lab, and I've solved your problem. And here's mine
package edu.calpoly.android.lab3;
import android.content.Context; import android.text.TextUtils.TruncateAt; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.TextView;
public class JokeView extends LinearLayout implements RadioGroup.OnCheckedChangeListener { 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 = " - ";
/**
* 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);
}
}
精彩评论