在這邊設計幾個簡單的BUTTON來改變畫面的顏色

單一BUTTON指令

XML:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在Main設定按件"
android:id="@+id/aaa1" />

JAVA:
Button inmainset = new Button(this);                     // 建立一個新Button
inmainset = (Button)findViewById(R.id.aaa1); // 指定連接到XML中id為aaa1的button物件
inmainset.setOnClickListener(new View.OnClickListener() {// 設定點擊
@Override
public void onClick(View v) {
background.setBackgroundColor(Color.RED); //畫面改變成紅色
}
});


可多選的BUTTON指令

XML:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="先在XML設定按件"
android:id="@+id/aaa2"
android:onClick="colortoBlue" /> //設定點擊時會連接到colortoBlue這個funtion
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="先在XML設定按件2"
android:id="@+id/aaa3"
android:onClick="colortoBlue" /> // 設定點擊時會連接到colortoBlue這個funtion

PS:這邊就先連接兩個可以同時很多的BUtton都連接同一個funtion

JAVA:
public void colortoBlue(View v2 ){
if( v2.getId() == R.id.aaa2 ) {
background.setBackgroundColor(Color.BLUE); // 變成藍色
}else if(v2.getId()==R.id.aaa3)
background.setBackgroundColor(Color.GREEN);// 變成綠色
}
 
偵測按住的BUTTON指令
XML:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按住"
android:id="@+id/aaa4"/>

JAVA:
要在開頭加
View.OnTouchListener
public class MainActivity extends ActionBarActivity implements View.OnTouchListener{
.
.
.
Button touchbutton = (Button)findViewById(R.id.aaa4) ;  設定新按鈕&指定連接到XML中id為aaa4的button物件
touchbutton.setOnTouchListener(this);                   連接onTouch
.
.
.
public boolean onTouch(View v3, MotionEvent e){
int temp = background.getSolidColor(); // 放原來的顏色資料
if( v3.getId() == R.id.aaa4 ) {
if (e.getAction() == MotionEvent.ACTION_DOWN) { // 按件按下
background.setBackgroundColor(Color.YELLOW); //變黃色
} // else
else if (e.getAction() == MotionEvent.ACTION_UP) { // 按鍵起來
background.setBackgroundColor(temp); // 回復原來的顏色

} // }else if( e.getAction() == MotionEvent.ACTION_UP )
}

return true ;
} // end boolean onTouch(View v, MotionEvent e)


完整XML:
<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="@+id/changcolor">


<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="變紅色"
android:id="@+id/textView1"
android:autoText="false"
android:textSize="50dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="在Main設定按件"
android:id="@+id/aaa1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="變藍色"
android:id="@+id/textView2"
android:textSize="50dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="先在XML設定按件1"
android:id="@+id/aaa2"
android:onClick="colortoBlue" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="變綠色"
android:id="@+id/textView3"
android:textSize="50dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="先在XML設定按件2"
android:id="@+id/aaa3"
android:onClick="colortoBlue" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按住變黃色"
android:id="@+id/textView4"
android:textSize="50dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按住"
android:id="@+id/aaa4"
android:singleLine="false" />
</LinearLayout>
</RelativeLayout>

完整JAVA:
package com.example.user.buttoncase;

import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.RelativeLayout;


public class MainActivity extends ActionBarActivity
implements View.OnTouchListener {

RelativeLayout background ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background = (RelativeLayout)findViewById(R.id.changcolor);
Button inmainset = new Button(this);
inmainset = (Button)findViewById(R.id.aaa1);
inmainset.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
background.setBackgroundColor(Color.RED);
}
});
Button touchbutton = (Button)findViewById(R.id.aaa4) ;
touchbutton.setOnTouchListener(this);

}


public void colortoBlue(View v2 ){
if( v2.getId() == R.id.aaa2 ) {
background.setBackgroundColor(Color.BLUE);
}else if(v2.getId()==R.id.aaa3)
background.setBackgroundColor(Color.GREEN);
}

public boolean onTouch(View v3, MotionEvent e){
int temp = background.getSolidColor();;
if( v3.getId() == R.id.aaa4 ) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
background.setBackgroundColor(Color.YELLOW);
} // else
else if (e.getAction() == MotionEvent.ACTION_UP) {
background.setBackgroundColor(temp);

} // }else if( e.getAction() == MotionEvent.ACTION_UP )
}

return true ;
} // end boolean onTouch(View v, MotionEvent e)


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}
 
 
       原始畫面                             第一個按鈕                               第二個按鈕

button1button2button3

                            第三個按鈕                              按住第四個按鈕                             第四個按鈕放開後

button4button5button6

 
  
 
 
arrow
arrow

    mountain0212 發表在 痞客邦 留言(0) 人氣()