|
|
|
Android ขอถามเรื่องการเพิ่มค่าในตัวแปรจากการใช้ button onClick หน่อยครับ |
|
|
|
|
|
|
|
Code (Android-Java)
package com.myapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.app.AlertDialog;
public class MainActivity extends Activity {
int iNum;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iNum = 0;
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
final Button btn1 = (Button) findViewById(R.id.button1);
// Perform action on click
btn1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
iNum = iNum + 1;
}
});
final Button btn2 = (Button) findViewById(R.id.button2);
// Perform action on click
btn2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
iNum = iNum - 1;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Go to : Button - Android Widgets Example
|
ประวัติการแก้ไข 2013-03-08 08:49:50
|
|
|
|
Date :
2013-03-08 06:26:53 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณมากครับ
|
|
|
|
|
Date :
2013-03-08 07:33:58 |
By :
makachol |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
มีผิดพลาดนิดหน่อย แก้ไขให้แล้วครับ
|
|
|
|
|
Date :
2013-03-08 08:50:16 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Code (Android-Java)
package app.account;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.R.integer;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class Tabbalance extends Activity{
int NumDay;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.totalmoney);
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
databalance();
}
private void databalance() {
// TODO Auto-generated method stub
NumDay = 0;
ImageButton btnBackDay = (ImageButton)findViewById(R.id.btnBackday);
btnBackDay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
NumDay = NumDay+1;
}
});
ImageButton btnNeckDay = (ImageButton)findViewById(R.id.btnNextday);
btnNeckDay.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
NumDay = NumDay-1;
Toast.makeText(getBaseContext(), NumDay, Toast.LENGTH_LONG).show();
}
});
final TextView txttotal = (TextView)findViewById(R.id.totalmoney);
final TextView txtinput = (TextView)findViewById(R.id.totalinput);
final TextView txtoutput = (TextView)findViewById(R.id.totaloutput);
final TextView txtsummary = (TextView)findViewById(R.id.totalsummary);
final TextView txtday = (TextView)findViewById(R.id.txtDay);
final String Day = (String.valueOf(NumDay));
Toast.makeText(getBaseContext(), Day, Toast.LENGTH_LONG).show();
// TODO Auto-generated method stub
Intent intent= getIntent();
final String Mid = intent.getStringExtra("MemberID");
String url = "----------------------------";
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("mid", Mid));
params.add(new BasicNameValuePair("mid", Day));
String Server = getJSONUrl(url, params);
String stramount = "" ;
String strinput = "";
String stroutput = "";
String strsummary = "";
String strday = "";
JSONObject c;
try {
c = new JSONObject(Server);
stramount = c.getString("amount");
strinput = c.getString("input");
stroutput = c.getString("output");
strsummary = c.getString("summary");
strday = c.getString("date");
if(!Mid.equals("")){
txttotal.setText(stramount);
txtinput.setText(strinput);
txtoutput.setText(stroutput);
txtsummary.setText(strsummary);
txtday.setText(strday);
}
else{
txttotal.setText("");
txtinput.setText("");
txtoutput.setText("");
txtsummary.setText("");
txtday.setText("");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public String getJSONUrl(String url,List<NameValuePair> params) {
StringBuilder str = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
try {
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = client.execute(httpPost);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) { // Download OK
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
str.append(line);
}
} else {
Log.e("Log", "Failed to download file..");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str.toString();
}
}
ขอถามต่ออีกหน่อยครับ พอดีผมเขียนโค้ดออกมามันคลิกปุ่มแลัวยัง error ครับ
ผมอยากให้ onclick นั้นเพิ่มค่า Numday ที่ละ1 หรือลด ทีละ หนึ่งแต่จะให้มันมันส่งพารามิเตอร์ที่เป็นตัวแปรเมื่อผมกดแล้วให้มันส่งข้อมูลจากพารามิเตอร์ที่ผมส่งไปหนะครับ มันยัง error รบกวนดูให้หน่อยนะครับ
|
|
|
|
|
Date :
2013-03-08 10:11:24 |
By :
makachol |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Error บรรทัดไหน Error ว่าอะไรครับ
|
|
|
|
|
Date :
2013-03-08 10:38:27 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 05
|