【发布时间】:2012-06-08 14:44:25
【问题描述】:
我知道这很容易,但我无法调试它。
我正在动态创建 3 个 LinearLayouts 并将它们添加到滚动视图中。我在 3 个线性布局的顶部添加了一个按钮“ins”。
问题是在点击 ins Button 时,它的 on clicklistener 被调用了 3 次。 代码:
package com.integrated.mpr;
import android.app.Activity;
import android.app.Dialog;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
public class Page1 extends Activity implements OnClickListener{
static int pos = new Choose().n;
static String partname;
int i;
int[][] id = new int[pos][5];
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
int p =0;
for(int i =0;i<3;i++){
for(int j =0;j<5;j++){
p++;
}
}
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
int resid = getResources().getIdentifier("background", "drawable", getPackageName());
ll.setBackgroundResource(resid);
Button ins = new Button(this);
ins.setText("Instructions");
ins.setOnClickListener(this);
ins.setId(5000);
ll.addView(ins);
for(i =0;i<3;i++){
LinearLayout llay = new LinearLayout(this);
llay.setOrientation(LinearLayout.VERTICAL);
TextView tv = new TextView(this);
tv.setText("enter the " +(i+1)+" position name");
EditText et = new EditText(this);
et.setId(id[i][0]);
Button starta = new Button(this);
starta.setText("Record 1");
starta.setId(id[i][1]);
starta.setOnClickListener(this);
Button startb = new Button(this);
startb.setText("Record 2");
startb.setId(id[i][2]);
startb.setOnClickListener(this);
Button startc = new Button(this);
startc.setText("Record 3");
startc.setId(id[i][3]);
startc.setOnClickListener(this);
Button stop = new Button(this);
stop.setText("Submit");
stop.setId(id[i][4]);
stop.setOnClickListener(this);
TextView tv1 = new TextView(this);
tv1.setVisibility(llay.INVISIBLE);
llay.addView(tv);
llay.addView(et);
llay.addView(starta);
llay.addView(startb);
llay.addView(startc);
llay.addView(stop);
llay.addView(tv1);
ll.addView(llay);
}
Button bcon = new Button(this);
bcon.setText("Continue");
bcon.setId(10000);
bcon.setOnClickListener(this);
ll.addView(bcon);
sv.addView(ll);
this.setContentView(sv);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==5000){
Log.d("ins", "called");
Context mContext = Page1.this;
Dialog dialog = new Dialog(mContext);
dialog.setTitle("Instructions");
dialog.setContentView(R.layout.instructiondialog);
dialog.show();
}
}
}
}
我肯定知道,OnCreate 的代码中存在一些问题,但它是什么?请帮忙
谁能进一步解释如何设置按钮对齐屏幕右侧,即如何设置其布局重力?
【问题讨论】:
-
我猜这与在 for 循环中声明它有关...
-
没错,但你能说得更准确一点
-
正如 GAMA 所说,如果您循环 3 次,并在每个循环中定义一个 onclick 侦听器,您将获得三个 onclicklistener,它们都会在用户按下按钮时被调用。
-
我没有在循环中定义ins按钮的onclick监听器,它在循环之外
-
@kumarpiyush 我认为您的代码没有问题,因为我复制粘贴了您的代码,它称为指令单击一次而不是三次