2015年8月6日 星期四

[Android]初學 使用Intent在Activity傳值(二)

Bundle的傳值,只是單純的A-->B , B-->C 單向傳值 

不像上一篇講到的AB互傳溝通

因此Bundle傳值時,給出的畫面是一個新的、乾淨的初始畫面

範例:


public class MainActivity extends AppCompatActivity {
private Button widget2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
widget2 =(Button)findViewById(R.id.btnNext2);
//接收SecondActivity bundle傳來的參數
Bundle param = getIntent().getExtras();
if(param !=null)
{
setTitle(param.getString("bparam"));
}
//利用bundle傳參數到ScondActivity
widget2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent2 = new Intent();
Bundle bundle= new Bundle();
bundle.putString("sourse","bundle from Main");
intent2.setClass(MainActivity.this, widgetActivity2.class);
intent2.putExtras(bundle);
startActivity(intent2);
}
});
}
}
view raw Main.java hosted with ❤ by GitHub
public class SecondActivity extends AppCompatActivity {
private Button prev;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.widget2);
//接收Main 傳來的參數
Bundle bundle = getIntent().getExtras();
if(bundle !=null)
{
setTitle(bundle.getString("sourse"));
}
prev =(Button)findViewById(R.id.button1);
//Bundle傳送參數到Main
prev.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(widgetActivity2.this, MainActivity.class);
// Bundle 傳送參數
Bundle param = new Bundle();
param.putString("bparam", "from widget2");
intent.putExtras(param);
startActivity(intent);
}
});
view raw Second.java hosted with ❤ by GitHub

[Android]初學 使用Inten在Activity傳值(一)

Main畫面:

點下widget1按鈕,會將 "from Main" 帶到 Second畫面

Second畫面 :

點上一頁按鈕,會回傳 "from widget1"到Main
如果不是點上一頁按鈕兒是透過其他方法回到前一頁,在Main會顯示 "無參數"

startActivityForResult()除了傳值之外,還會回傳一個requestCode
所以Main要加入onActivityResult()方法,來接收辨別是哪個Activity回傳的requestCode


參數一定是從A-->B , B-->A AB兩個Activity在溝通
傳回參數的時候,只會接收參數,畫面不會刷新成預設畫面


最後要記得在AndroidManifest.xml 註冊 Activity
  1. <activity android:name=".SecondActivityName"></activity>


public class MainActivity extends AppCompatActivity {
private Button widget1;
private int REQUEST_CODE;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
widget1 =(Button)findViewById(R.id.btnNext);
//下一頁
widget1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent1 = new Intent();
intent1.setClass(MainActivity.this, widgetActivity.class);
intent1.putExtra("activityMain", "from Main");
//當 REQUEST_CODE =-1 等於執行 startActivity
// widgetActivity 回傳資料要使用 setResult
startActivityForResult(intent1,REQUEST_CODE);
//startActivity(intent1);
//MainActivity.this.finish();
}
});
}
//接收回傳值的 Function
@Override
protected void onActivityResult(int requestCode , int resultCode,Intent data)
{
if(requestCode == REQUEST_CODE)
{
if (resultCode == RESULT_OK){
String temp=null;
Bundle extras = data.getExtras();
if(extras != null)
{
temp = extras.getString("widget");
}
setTitle(temp);
}else
{
setTitle("無參數");
}
}
}
}
public class widgetActivity extends AppCompatActivity {
private Button prev;
private String data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.widget);
prev=(Button)findViewById(R.id.btnPrev);
Bundle extras =getIntent().getExtras();
if(extras !=null)
{
data=extras.getString("activityMain");
setTitle("widget :" + data);
}else
{
setTitle("widget");
}
prev.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v)
{
Intent intent =new Intent();
intent.setClass(widgetActivity.this,MainActivity.class);
// Bundle 傳遞 Data
Bundle bundle = new Bundle();
bundle.putString("widget","from widget1");
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
finish();
}
});
}

2015年8月5日 星期三

[Android]初學-控制項應用



  1. 按下按鈕,顯示EditView輸入的內容和選到的CheckBox
  2. 顯示框出現,利用RadioGroup選擇Show出訊息或隱藏訊息
  3. 按下清除按鈕,EditView、CheckBox 回復初始狀態

public class MainActivity extends AppCompatActivity {
private Button press;
private Button clear;
private TextView Name;
private TextView msg;
private ArrayList<CheckBox> cbList = new ArrayList<CheckBox>();
private RadioGroup rGroup;
private RadioButton btnShow;
private RadioButton btnHide;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();
SetDefault();
//msg內容
press.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v){
String r="";
//第一種CheckBox批次跑迴圈寫法
int ckID[]={R.id.checkBox1,R.id.checkBox2,R.id.checkBox3,R.id.checkBox4,R.id.checkBox5,R.id.checkBox6};
CheckBox a =null;
for(int i=0 ;i<ckID.length;i++)
{
if((a=(CheckBox)findViewById(ckID[i]))!=null)
{
if(a.isChecked())
{
r+=a.getText()+",";
}
}
}
setTitle("MyApp1 Name:" + Name.getText());
msg.setText("checked: " + r.substring(0,r.length()-1) );
rGroup.setVisibility(View.VISIBLE);
}
});
//控制 msg可見
rGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == btnHide.getId()) {
msg.setVisibility(View.GONE);
} else if (checkedId == btnShow.getId()) {
msg.setVisibility(View.VISIBLE);
}
}
});
}
private void findView(){
msg=(TextView)findViewById(R.id.textView4);
press = (Button)findViewById(R.id.button1);
clear=(Button)findViewById(R.id.btnclear);
Name=(TextView)findViewById(R.id.editText);
rGroup=(RadioGroup)findViewById(R.id.rGroup);
btnShow =(RadioButton)findViewById(R.id.radioButton1);
btnHide =(RadioButton)findViewById(R.id.radioButton2);
CheckBox ck1 =(CheckBox)findViewById(R.id.checkBox1);
CheckBox ck2 =(CheckBox)findViewById(R.id.checkBox2);
CheckBox ck3 =(CheckBox)findViewById(R.id.checkBox3);
CheckBox ck4 =(CheckBox)findViewById(R.id.checkBox4);
CheckBox ck5 =(CheckBox)findViewById(R.id.checkBox5);
CheckBox ck6 =(CheckBox)findViewById(R.id.checkBox6);
cbList.add(ck1);
cbList.add(ck2);
cbList.add(ck3);
cbList.add(ck4);
cbList.add(ck5);
cbList.add(ck6);
}
//清除
private void SetDefault(){
clear.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
//第二種CheckBox批次跑迴圈寫法
//批次取消CheckBox選項
for (CheckBox c: cbList) {
c.setChecked(false);
}
msg.setText("Click Clear");
Name.setText("");
setTitle("MyApp1");
msg.setVisibility(View.VISIBLE);
rGroup.setVisibility(View.GONE);
}
});
}
//msg可見
private void SetMsgVisiable(){
}
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);
}
}
view raw .java hosted with ❤ by GitHub
<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: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=".MainActivity">
<TextView
android:id="@+id/txt1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoText="false"
android:text="@string/hello_world"
/>
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:layout_marginTop="29dp"
android:hint="請輸入姓名"
android:layout_below="@+id/txt1"
android:layout_centerHorizontal="true" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="帳號"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_alignBaseline="@+id/editText"
android:layout_alignBottom="@+id/editText"
android:layout_alignParentStart="true" />
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="寫程式"
android:textSize="@dimen/abc_text_size_small_material"
android:layout_below="@+id/editText"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/checkBox1"
android:layout_marginStart="23dp"
android:layout_toEndOf="@+id/checkBox1"
android:checked="false"
android:text="聽音樂" />
<CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/checkBox2"
android:layout_marginStart="30dp"
android:layout_toEndOf="@+id/checkBox2"
android:checked="false"
android:text="看書" />
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="顯示"
android:layout_centerVertical="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除"
android:id="@+id/btnclear"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/textView4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#405dff"
android:layout_below="@+id/rGroup"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnclear"
android:layout_alignParentStart="true"
android:id="@+id/rGroup"
android:visibility="invisible">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="show msg"
android:id="@+id/radioButton1"
android:checked="true"
/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hide msg"
android:id="@+id/radioButton2"
android:layout_below="@+id/btnclear"
android:layout_alignParentStart="true"
android:checked="false"
/>
</RadioGroup>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="運動 "
android:id="@+id/checkBox4"
android:layout_below="@+id/checkBox1"
android:layout_alignParentStart="true"
android:checked="false" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡覺"
android:id="@+id/checkBox5"
android:checked="false"
android:layout_below="@+id/checkBox2"
android:layout_alignStart="@+id/checkBox2" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="看電影"
android:id="@+id/checkBox6"
android:checked="false"
android:layout_below="@+id/checkBox3"
android:layout_toEndOf="@+id/checkBox5"
android:layout_alignStart="@+id/checkBox3" />
</RelativeLayout>
view raw .xml hosted with ❤ by GitHub





2015年8月3日 星期一

[Android] 基本Layout屬性



版面方向

android:orientation vertical :縱向 版面由上到下horizontal:橫向

寬高設定

android:layout_width
android:layout_height

fill_parent:寬或高滿版
wrap_content:寬或高自動調整,顯示完整內容
match_parent:同fill_parent

設定View的相對位置

android:layout_gravity

設定View內容的位置

android:gravity