我的java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
RadioGroup groupRadio=(RadioGroup)findViewById(R.id.radioGroup1);
final EditText squareedt=(EditText)findViewById(R.id.SquareEditText);
final EditText reclengthedt=(EditText)findViewById(R.id.RecLengthEditText);
final EditText recwidthedt=(EditText)findViewById(R.id.RecWidthEditText);
final EditText circleedt=(EditText)findViewById(R.id.CircleEditText);
final TextView side=(TextView) findViewById(R.id.LabelSide);
final TextView length=(TextView) findViewById(R.id.LabelLength);
final TextView width=(TextView) findViewById(R.id.LabelWidth);
final TextView radius=(TextView) findViewById(R.id.LabelRadius);
squareedt.setVisibility(View.INVISIBLE);
reclengthedt.setVisibility(View.INVISIBLE);
recwidthedt.setVisibility(View.INVISIBLE);
circleedt.setVisibility(View.INVISIBLE);
side.setVisibility(View.INVISIBLE);
length.setVisibility(View.INVISIBLE);
width.setVisibility(View.INVISIBLE);
radius.setVisibility(View.INVISIBLE);
groupRadio.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId==R.id.rbt1)
{
side.setVisibility(View.VISIBLE);
squareedt.setVisibility(View.VISIBLE);
float una = Float.parseFloat(squareedt.getText().toString());
float perimeter = una * una;
Toast.makeText(this, String.valueOf("The perimeter of square is " + perimeter),Toast.LENGTH_LONG).show();
length.setVisibility(View.INVISIBLE);
width.setVisibility(View.INVISIBLE);
reclengthedt.setVisibility(View.INVISIBLE);
recwidthedt.setVisibility(View.INVISIBLE);
radius.setVisibility(View.INVISIBLE);
circleedt.setVisibility(View.INVISIBLE);
}
else if(checkedId==R.id.rbt2)
{
length.setVisibility(View.VISIBLE);
width.setVisibility(View.VISIBLE);
reclengthedt.setVisibility(View.VISIBLE);
recwidthedt.setVisibility(View.VISIBLE);
side.setVisibility(View.INVISIBLE);
squareedt.setVisibility(View.INVISIBLE);
radius.setVisibility(View.INVISIBLE);
circleedt.setVisibility(View.INVISIBLE);
}
else if(checkedId==R.id.rbt3)
{
radius.setVisibility(View.VISIBLE);
circleedt.setVisibility(View.VISIBLE);
length.setVisibility(View.INVISIBLE);
width.setVisibility(View.INVISIBLE);
reclengthedt.setVisibility(View.INVISIBLE);
recwidthedt.setVisibility(View.INVISIBLE);
side.setVisibility(View.INVISIBLE);
squareedt.setVisibility(View.INVISIBLE);
}
}
});
}
}
我总是遇到这个代码的问题
Toast.makeText(this, String.valueOf("The perimeter of square is " + perimeter),Toast.LENGTH_LONG).show();
错误是这样的; Toast类型中的方法makeText(Context,CharSequence,int)不适用于参数(new RadioGroup.OnCheckedChangeListener(){},String,int)
我的xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.cell.phone.MainActivity$PlaceholderFragment" >
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RadioButton
android:id="@+id/rbt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/square" />
<RadioButton
android:id="@+id/rbt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/rectangle" />
<RadioButton
android:id="@+id/rbt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/circle" />
</RadioGroup>
<TextView
android:id="@+id/LabelSide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/radioGroup1"
android:layout_below="@+id/radioGroup1"
android:layout_marginLeft="15dp"
android:layout_marginTop="22dp"
android:text="@string/side"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/SquareEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/LabelSide"
android:layout_alignBottom="@+id/LabelSide"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/LabelSide"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/RecLengthEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/LabelSide"
android:layout_alignBottom="@+id/LabelSide"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/LabelSide"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/LabelLength"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/LabelSide"
android:layout_alignBottom="@+id/LabelSide"
android:layout_alignLeft="@+id/radioGroup1"
android:text="@string/length"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/RecWidthEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/SquareEditText"
android:layout_below="@+id/SquareEditText"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/LabelWidth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/RecWidthEditText"
android:layout_alignBottom="@+id/RecWidthEditText"
android:layout_alignLeft="@+id/radioGroup1"
android:text="@string/width"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="@+id/CircleEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/LabelSide"
android:layout_alignBottom="@+id/LabelSide"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/LabelSide"
android:ems="10"
android:inputType="number" />
<TextView
android:id="@+id/LabelRadius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/LabelSide"
android:layout_alignBottom="@+id/LabelSide"
android:layout_alignLeft="@+id/radioGroup1"
android:text="@string/radius"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/RecWidthEditText"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:gravity="center_vertical|center_horizontal|center"
android:text="" />
答案 0 :(得分:0)
变化:
Toast.makeText(this, String.valueOf("The perimeter of square is " + perimeter),Toast.LENGTH_LONG).show();
要:
Toast.makeText(MainActivity.this, String.valueOf("The perimeter of square is " + perimeter),Toast.LENGTH_LONG).show();
this
此处引用的OnCheckedChangeListener不适用于makeText(Context, CharSequence, int)
。 MainActivty.this
是上下文。
答案 1 :(得分:0)
尝试替换
Toast.makeText(this, String.valueOf("The perimeter of square is " + perimeter),Toast.LENGTH_LONG).show();
通过
Toast.makeText(getApplicationContext(), "The perimeter of square is " + perimeter,Toast.LENGTH_LONG).show();
OR
Toast.makeText(MainActivity.this, "The perimeter of square is " + perimeter,Toast.LENGTH_LONG).show();