我是Eclipse的新手,我试图创建一个Android应用程序,如果我单击"创建按钮"它将创建一个文本字段和另一个具有ID的按钮(对于&#34中的每次点击,ID + 1;创建按钮")
如果我点击创建的按钮,任何用textfield编写的内容都会保存在数据库中。
这是我的代码
package com.example.pmkrj;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class monitortask extends Activity {
Button bb11;
EditText tf1;
LinearLayout.LayoutParams params ;
LinearLayout linearlayout ;
int i = 0 ;
String m = "Monitor Task", td = "Save!";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.monitortask);
bb11 = (Button)findViewById(R.id.bb1);
linearlayout = (LinearLayout)findViewById(R.id.linear_lay);
params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(0, 5, 0, 5);
bb11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)`enter code here` {
// TODO Auto-generated method stub
tf1 = new EditText(getApplicationContext());
bb11 = new Button (getApplicationContext());
bb11.setText(String.valueOf(m + " " +1));
bb11.setText(String.valueOf(td));
bb11.setId(i+1);
++i ;
linearlayout.addView(tf1, params);
linearlayout.addView(bb11, params);
}
});
}
public void onClick(View arg0) {
bb11 = (Button)findViewById(R.id.bb1);
bb11.setOnClickListener((OnClickListener) this);
switch (arg0.getId()) {
case R.id.bb1:
boolean didItWork = true;
try {
String date = tf1.getText().toString();
String task = bb11.getText().toString();
Member entry = new Member(this);
entry.open();
entry.createInfo(date, task);
entry.close();
} catch (Exception e ) {
didItWork = false;
Dialog d = new Dialog(this);
d.setTitle("Sorry!");
TextView tv = new TextView(this);
tv.setText("\tFailed");
d.setContentView(tv);
d.show();
}finally {
if (didItWork) {
Dialog d = new Dialog(this);
d.setTitle("Awesome!");
TextView tv = new TextView(this);
tv.setText("\tSuccess");
d.setContentView(tv);
d.show();
}
}
break;
}
}
}