目前分類:android (2)

瀏覽方式: 標題列表 簡短摘要

在JAVA 程式註冊 Broadcast Receiver 的寫法 :

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=".MyActivity">

<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/EditTxt"
android:textSize="30sp"
android:text=""
/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="確定"
android:textSize="30sp"
android:layout_below="@+id/EditTxt"
/>

<TextView
android:id="@+id/txtView"
android:layout_below="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

-----------------------------------------------------

JAVA: MyActivity

 

package com.example.user.test;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.view.View.OnClickListener;

public class MyActivity extends Activity{

Button btnTest;
EditText editTextTest;
public TextView txtViewTest;
String stringTxt;

TestBroadcaseReceiver testBroadcaseRecTest;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);

(btnTest = (Button)findViewById(R.id.btn1)).setOnClickListener(btnOnClickLinter);
editTextTest = (EditText)findViewById(R.id.EditTxt);
txtViewTest = (TextView)findViewById(R.id.txtView);

stringTxt = editTextTest.getText().toString();

// TestBroadcaseReceiver 註冊
IntentFilter itFile = new IntentFilter(stringTxt);
testBroadcaseRecTest = new TestBroadcaseReceiver();
registerReceiver(testBroadcaseRecTest, itFile);

}
OnClickListener btnOnClickLinter = new OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()){
case R.id.btn1:
test();
}
}
};
public void test() {

//把EditText 的資料指派到strText
String strText = editTextTest.getText().toString();

if(strText.trim().equals("")) {
editTextTest.setError("請輸入訊息");
}else {

//把Intent(stringTxt)數值存職之後判斷"安安"SetText 存直editText直傳給BroadcaseReceiver class
sendBroadcast(new Intent(stringTxt).putExtra("安安", strText));
txtViewTest.setText(stringTxt);
}
}
}

-------------------------------------------------------------

JAVA: TestBroadcaseReceiver

package com.example.user.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

/**
* Created by user on 2014/10/30.
*/
public class TestBroadcaseReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {

//intent.getStringExtra("安安") 判斷 putExtra ("安安")取得存值得值
String snder = intent.getStringExtra("安安");
Toast.makeText(context,"收到訊息:"+snder,Toast.LENGTH_SHORT).show();
}
}

------------------------------------------------------

執行結果:

android 執行結果未命名a  

文章標籤

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

android:

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" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="放入購物車" />
 
    <TextView
        android:id="@+id/TextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1" />
 
    <TextView
        android:id="@+id/TextView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView1" />
 
    <ListView
        android:id="@+id/lstView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/TextView2" />
 
</RelativeLayout>
 
-----------------------------------------------
 
java:
package com.example.test18;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
 
public class MainActivity extends Activity implements OnClickListener,
OnItemClickListener {
 
public Button btn1;
public TextView txtView1, txtView2;
public ListView lis;
 
String[] str = new String[] { "香蕉", "西瓜", "梨子", "水蜜桃" };
String[] b = new String[] { "330", "120", "250", "280" };
 
int[] a = new int[] { 330, 120, 250, 280 };
int i;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lis = (ListView) findViewById(R.id.lstView);
txtView1 = (TextView) findViewById(R.id.TextView1);
txtView2 = (TextView) findViewById(R.id.TextView2);
(btn1 = (Button) findViewById(R.id.button1)).setOnClickListener(this);
 
ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_multiple_choice, str);
 
lis.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lis.setAdapter(arrayadapter);
i = arrayadapter.getCount();
lis.setOnItemClickListener(this);
 
}
 
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
 
public void onClick(View v) {
// TODO Auto-generated method stub
int sellAll = 0;
for (int x = 0; x < i; x++)
if (lis.isItemChecked(x))
sellAll += a[x];
txtView2.setText("總額:" + sellAll);
 
}
 
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
if (lis.isItemChecked(arg2)) {
 
String sel = arg0.getItemAtPosition(arg2).toString();
 
setTitle("選購了:" + sel);
 
} else {
setTitle("");
}
 
}
}
 

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