-
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:
- Không kết nối được với Google API trong Android
- Tìm Source Code Android OS 4.4.2
- Xin source code ứng dụng đọc file .prc Nguồn bài viết : DroidViet.Com Xin source code ứng dụng đọc file .prc
- Hiệu suất của android app khi viết bằng C++ hay Java thế nào?
- 8 Bài hướng dẫn lập trình android cơ bản Tiếng Việt
- Các hàm lấy thông tin về ngày sinh và giới tính trong forum
- Sách hay về Android(Professional Android 4 Application Development)
- Cách thế nào để httptransportSE hết timeout?
- Tài liệu lập trình java trên symbian s60v5
- Xây dựng chương trình hát Karaoke trên mobile bằng j2me, có thể không?
-
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
Giảm giá khi mua pallet nhựa Trà Vinh Tại Trà Vinh, một trong những địa điểm nổi tiếng với ngành nông nghiệp và sản xuất, pallet nhựa Trà Vinh đóng vai trò không thể phủ nhận trong quá trình vận...
Giảm giá khi mua pallet nhựa Trà...