我是 android 编程新手,我正在制作一个程序,当您按下三个单选按钮之一时,它会更改 textview 项目的颜色。每个单选按钮对应于某种特定颜色。代码编译并且没有给出任何错误,但它没有执行。这是代码
package org.example.project2;
import android.os.Bundle;
import android.view.View;
import android.app.Activity;
import android.view.Menu;
import android.widget.RadioButton;
import android.widget.EditText;
//import android.view.View.OnClickListener;
import android.graphics.Color;
public class MainActivity extends Activity {
View mColorArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void ChangeState(View clickedbutton)
{
mColorArea=findViewById(R.id.color_place);
RadioButton RB=(RadioButton)clickedbutton;
CharSequence choice = RB.getText();
//String Choice1=choice.toString(); this is right too
String ChoiceString=getString(R.string.choice_string);
String Choice1 =
String.format(ChoiceString, choice);
if (Choice1=="Red")
{
mColorArea.setBackgroundColor(Color.RED);
//mColorArea.setText("red");
}
else if (Choice1=="Yellow")
{
mColorArea.setBackgroundColor(Color.YELLOW);
//mColorArea.setText("yellow");
}
else
{
mColorArea.setBackgroundColor(Color.BLUE);
//mColorArea.setText("blue");
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
这是xml部分
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/red_button"
android:onClick="ChangeState"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/blue_button"
android:onClick="ChangeState"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/yellow_button"
android:onClick="ChangeState"
/>
</LinearLayout>
<TextView
android:id="@+id/color_place"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>