●實作練習:預期畫面
●程式說明:
(一)Radion button屬性
=>buttonTint:選鈕顏色
=>textcolor:文字顏色
=>checked:是否被選
=>textApperarance:文字大小
(二)取得radiobutton的文字與文字顏色
getText()
getCurrentTextColor(): Return the current color selected for normal text.
(三)設定背景顏色和文字顏色
setBackgroundColor(int bg): Set background behind animation.
參數:
int: The background color. If 0, no background. Currently must be black, with any desired alpha level.setTextColor(int color):Sets the text color for all the states (normal, selected, focused) to be this color.
參數:
int: A color value in the form 0xAARRGGBB. Do not pass a resource (做法是0xAARRGGBB(AA代表透明度alpha,不透明為ff))(四)設定radio button的選取狀態setChecked:Changes the checked state of this button
(五)manifeast的activity設定
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".colorPick"></activity>
</application>
=>這裡可以看到這個專案有兩個activity,負責啟動的那一個裡面有子標籤< intent-filter >,所以希望哪個activity啟動就要把這個子標籤的內容放進那個activity,若兩個activity都有這個子標籤,下載app安裝後,就會有兩個啟動的icon負責啟動不同的activity
●實作練習:xml
(一)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.student.lab04.MainActivity">
<TextView
android:id="@+id/initext"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:gravity="center"
android:text="顏色未設定"
android:textSize="25sp"
android:textColor="@android:color/background_dark"/>
<Button
android:id="@+id/gotopick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:textSize="16sp"
android:text="選擇顏色"
android:onClick="gotopick"/>
</LinearLayout>
(二)activity_color.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="130dp"
tools:context="com.example.student.lab04.colorPick">
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<!--預設紅色被選取-->
<RadioButton
android:id="@+id/red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:buttonTint="@android:color/holo_red_light"
android:checked="true"
android:onClick="picked"
android:text="holo_red_light"
android:textColor="@android:color/holo_red_light"
android:textSize="20sp" />
<RadioButton
android:id="@+id/orange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:buttonTint="@android:color/holo_orange_dark"
android:onClick="picked"
android:text="holo_orange_dark"
android:textColor="@android:color/holo_orange_dark"
android:textSize="20sp" />
<RadioButton
android:id="@+id/green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:buttonTint="@android:color/holo_green_light"
android:onClick="picked"
android:text="holo_green_light"
android:textColor="@android:color/holo_green_light"
android:textSize="20sp" />
<RadioButton
android:id="@+id/blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:buttonTint="@android:color/holo_blue_dark"
android:onClick="picked"
android:text="holo_blue_dark"
android:textColor="@android:color/holo_blue_dark"
android:textSize="20sp" />
</RadioGroup>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="cancel"
android:text="CANCEL" />
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="ok"
android:text="OK" />
</LinearLayout>
</LinearLayout>
●實作練習:Java Code
(一)MainActivity.java
public class MainActivity extends AppCompatActivity {
Button gotopick;
//因為有另一個class要取得color和text的值,
//並且設定initext,所以這三個都要是static
static TextView initext;
static int color=0;
static CharSequence text="";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gotopick =(Button)findViewById(R.id.gotopick);
initext =(TextView)findViewById(R.id.initext);
}
public void gotopick(View view){
Intent in=new Intent();
//可以用this取代MainAcitvity.this
in.setClass(this,colorPick.class);
startActivity(in);
}
}
(二)colorPick.java
public class colorPick extends AppCompatActivity {
Button cancel,ok;
RadioButton red,orange,green,blue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_color_pick);
cancel =(Button)findViewById(R.id.cancel);
ok =(Button)findViewById(R.id.ok);
red =(RadioButton)findViewById(R.id.red);
green =(RadioButton)findViewById(R.id.green);
orange =(RadioButton)findViewById(R.id.orange);
blue =(RadioButton)findViewById(R.id.blue);
}
public void picked(View view){
if(red.isChecked()){
//取得radio button的文字顏色丟給color
//(因為是屬於MainAcitvity的,所以要用MainActivity.color)
MainActivity.color=red.getCurrentTextColor();
//取得radio button的文字丟給text
MainActivity.text=red.getText();
//設定initext這個textview的背景顏色
MainActivity.initext.setBackgroundColor(MainActivity.color);
//設定initext這個textview的文字
MainActivity.initext.setText(MainActivity.text);
//顏色表示方式:0xAARRGGBB(AA代表透明度alpha,不透明為ff)
MainActivity.initext.setTextColor(0xffffffff);
}
if(orange.isChecked()){
MainActivity.color=orange.getCurrentTextColor();
MainActivity.text=orange.getText();
MainActivity.initext.setBackgroundColor(MainActivity.color);
MainActivity.initext.setText(MainActivity.text);
MainActivity.initext.setTextColor(0xffffffff);
}
if(green.isChecked()){
MainActivity.color=green.getCurrentTextColor();
MainActivity.text=green.getText();
MainActivity.initext.setBackgroundColor(MainActivity.color);
MainActivity.initext.setText(MainActivity.text);
MainActivity.initext.setTextColor(0xffffffff);
}
if(blue.isChecked()){
MainActivity.color=blue.getCurrentTextColor();
MainActivity.text=blue.getText();
MainActivity.initext.setBackgroundColor(MainActivity.color);
MainActivity.initext.setText(MainActivity.text);
MainActivity.initext.setTextColor(0xffffffff);
}
}
public void cancel(View view){
//setChecked:Changes the checked state of this button
//所以恢復到紅色被選擇的狀態
red.setChecked(true);
}
public void ok(View view){
picked(view);
//Call this when your activity is done and should be closed
finish();
}
}
*程式參考(gitHub):RadioButton與跨activity傳送資料實作練習
文章標籤
全站熱搜
