-
28-04-2012, 08:01 AM #1Silver member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Cách tạo một button mới trong lập trình Android, lỗi: "unexpected" Force close!
đây là File Activity.java
Mã:package com.myApp; import android.R.layout;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.LinearLayout; public class CreatDynamicButtonActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // edtX = (EditText)findViewById(R.id.edtX); //x = Integer.parseInt(edtX.getText().toString()); // //EditText edtY = (EditText)findViewById(R.id.edtY); //y = Integer.parseInt(edtY.getText().toString()); // final Button btnCreatButton = (Button)findViewById(R.id.btnCreatButton); final LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1); final Button newButton = new Button(this); final EditText edtRong = (EditText)findViewById(R.id.edtRong); // final EditText edtDai = (EditText)findViewById(R.id.edtDai); btnCreatButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub final int x,y,rong,dai ; rong = Integer.parseInt(edtRong.getText().toString()); dai = Integer.parseInt(edtDai.getText().toString()); newButton.setText("NewButton"); newButton.setWidth(rong); newButton.setHint(dai); layout1.addView(newButton); } }); }}
Mã:<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="vi tri x: " /> <EditText android:id="@+id/edtX" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="vi tri y: " /> <EditText android:id="@+id/edtY" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="chieu rong: " /> <EditText android:id="@+id/edtRong" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chieu Dai: " /> <EditText android:id="@+id/edtDai" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/btnCreatButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="CreatButton" /> <LinearLayout android:id="@+id/layout1" android:layout_width="wrap_content" android:layout_height="wrap_content" ></LinearLayout>></LinearLayout>
View more random threads:
- Cách lưu trữ và hiển thị đoạn văn bản trên Android?
- Lỗi không đọc được dữ liệu từ phía sever
- sử dụng ffmpeg để lập trình với android?
- Tạo một đối tượng trong Java căn bản
- Làm sao để stop thread trong android ?
- Binh Anh Elcectronics tuyển dụng Lập trình viên Android
- Lấy giá trị gps từ điện thoại di động vào máy tính dùng ngôn ngữ c
- Cách code sự kiện click Button "chia sẻ ảnh chụp màn hình" giống game bắt chữ hoặc flappy bird?
- Cơ chế tự update app Android là như thế nào?
- Tăng tốc khi test android apps tự viết trên virtualbox
-
28-04-2012, 10:00 AM #2Banned
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Bỏ cái dòng: final Button newButton = new Button(this); đi xem nào?
Nếu muốn tạo button trong android bằng code thì bạn phải làm tối thiểu như thế này:
Mã:Button newButton = new Button(this);newButton.setText("CongdongCViet.com");layout1.addView(newButton);
-
28-04-2012, 10:41 AM #3Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Bạn nên tìm hiểu tài liệu lập trình java/android căn bản trước rồi hãy làm. Bạn hỏi một câu hỏi rất ngớ ngẩn như trên thì không ai muốn trả lời cho bạn => bạn sẽ mãi mãi không bao giờ có câu trả lời, mà người trả lời đọc cũng thấy ngán ngẩm.
Một vấn đề nữa mà bạn nên học hỏi là: "Hãy đọc kỹ hướng dẫn trước khi sử dụng". Trong khi Kevin đã nói rõ rằng: Nếu muốn tạo button trong android bằng code thì bạn phải làm tối thiểu như thế này: ...
-
02-05-2012, 08:04 AM #4Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
Bạn Import cái này:
import android.view.View.OnClickListener;
-
02-05-2012, 06:29 PM #5Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
code android nên dùng eclipse, đừng dùng netbeans nhé bạn [IMG]images/smilies/alert.gif[/IMG]
-
04-05-2012, 06:18 PM #6Junior Member
- Ngày tham gia
- Sep 2015
- Bài viết
- 0
nếu muốn làm game thì vớ tạm 1 cái thư viện nào mà code , có java console là đủ chiến rồi , nó chả liên quan gì đến mấy cái control này đâu
Thông qua phân tích dữ liệu Google từ 86 quốc gia, mới đây, một công ty tại Anh đã công bố bảng xếp hạng kích tấc "cậu nhỏ" của các nước trên thế giới. Kết quả, hầu hết các nước xếp ở nhóm đầu của...
"Chim" của chàng trai Việt thuộc...