<?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>
我有类扩展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();
}
}
但点击按钮
时会发生onClick答案 0 :(得分:0)
问题有点模糊。但我怀疑,因为你没有为按钮点击调用提供onClick方法,它默认为它能找到的唯一一个。试试这个:
android:onClick="expandButtonClicked"
然后提供编译器查找的方法。
答案 1 :(得分:0)
当然,当您设置 m_vwExpandButton.setOnClickListener 时,它会变得可点击!
官方文档说明:
setOnClickListener - 注册单击此视图时要调用的回调。 如果此视图无法点击,则会变为可点击。
答案 2 :(得分:0)
我正在做这个实验室,我已经解决了你的问题。 这是我的
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;
公共类JokeView扩展LinearLayout实现了RadioGroup.OnCheckedChangeListener { private Button m_vwExpandButton; 私人RadioButton m_vwLikeButton; 私人RadioButton m_vwDislikeButton; private RadioGroup m_vwLikeGroup; private TextView m_vwJokeText; 私人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);
}
}