Android Can';是否使用for循环来设置按钮或文本视图的ClickListener?

Android Can';是否使用for循环来设置按钮或文本视图的ClickListener?,android,textview,Android,Textview,我有很多可点击的文本视图/按钮。如果我必须键入以下内容,这只会使开发过程变得单调乏味: OnCreate... etc, etc. tvOutput0.setOnClickListener(this); tvOutput1.... tvOutput2..... ... tvOutput100.setOnClickListener(this) 我试图实现一个for循环 tvOutputArray[] = {tvOutput0, tvOutput1,.

我有很多可点击的文本视图/按钮。如果我必须键入以下内容,这只会使开发过程变得单调乏味:

OnCreate... etc, etc.
    tvOutput0.setOnClickListener(this);
    tvOutput1....
    tvOutput2.....
    ...
    tvOutput100.setOnClickListener(this)
我试图实现一个for循环

   tvOutputArray[] = {tvOutput0, tvOutput1,....TvOutput100}
   for(int i=0; i< tvOutputArray.length-1;i++){
         tvOutputArray[i].setOnClickListener(this);
   }
tvOutputArray[]={tvOutput0,tvOutput1,….TvOutput100}
for(int i=0;i
唉,它每次都会崩溃。我尝试这样做。除了手工(即每一个都打出来)之外,还有别的方法吗

  12-29 08:42:58.120: ERROR/StrictMode(597): null
    android.app.ServiceConnectionLeaked: Service com.android.exchange.ExchangeService        has leaked ServiceConnection com.android.emailcommon.service.ServiceProxy$ProxyConnection@40cd0fb0 that was originally       bound here
    at android.app.LoadedApk$ServiceDispatcher.<init>(LoadedApk.java:969)
    at android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
    at android.app.ContextImpl.bindService(ContextImpl.java:1418)
    at android.app.ContextImpl.bindService(ContextImpl.java:1407)
    at android.content.ContextWrapper.bindService(ContextWrapper.java:473)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
    at com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
    at com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
    at com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:551)
    at com.android.emailcommon.utility.Utility$2.doInBackground(Utility.java:549)
    at android.os.AsyncTask$2.call(AsyncTask.java:287)
    at java.util.concurrent.FutureTask.run(FutureTask.java:234)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
    at java.lang.Thread.run(Thread.java:856)
12-29 08:42:58.120:错误/严格模式(597):空
android.app.ServiceConnectionLeaked:Service com.android.exchange.ExchangeService已泄漏ServiceConnection com.android.emailcommon.Service.ServiceProxy$ProxyConnection@40cd0fb0原来是订在这里的
在android.app.LoadedApk$ServiceDispatcher上。(LoadedApk.java:969)
位于android.app.LoadedApk.getServiceDispatcher(LoadedApk.java:863)
位于android.app.ContextImpl.bindService(ContextImpl.java:1418)
位于android.app.ContextImpl.bindService(ContextImpl.java:1407)
位于android.content.ContextWrapper.bindService(ContextWrapper.java:473)
位于com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:157)
位于com.android.emailcommon.service.ServiceProxy.setTask(ServiceProxy.java:145)
位于com.android.emailcommon.service.ServiceProxy.test(ServiceProxy.java:191)
位于com.android.exchange.ExchangeService$7.run(ExchangeService.java:1850)
位于com.android.emailcommon.utility.utility$2.doInBackground(utility.java:551)
位于com.android.emailcommon.utility.utility$2.doInBackground(utility.java:549)
在android.os.AsyncTask$2.call(AsyncTask.java:287)
在java.util.concurrent.FutureTask.run(FutureTask.java:234)处
位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
运行(Thread.java:856)
用于(int i=0;i
你忘了我++

 for(int i=0; i< tvOutputArray.length-1;i++)
for(int i=0;i
我认为您正在尝试这样做:

// we need array of view's id
int [] viewIds = new int [] {R.id.tv1, R.id.tv2, R.id.bt1...};

// loop through ids, find view, set listener
for (int i = 0; i < viewIds.lenght; i++){
     View v = findViewById(viewIds[i]);
     if (v != null) {
         v.setOnClickListener(this);
     }
}
//我们需要视图id的数组
int[]viewIds=newint[]{R.id.tv1,R.id.tv2,R.id.bt1…};
//通过ID循环,查找视图,设置侦听器
对于(int i=0;i
我认为您必须以编程方式使用create按钮/Textview

public class ActivityName extends Activity implements OnClickListener

{

 private static final int MY_BUTTON = 9000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
        LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);
 
        // add text view
        TextView tv = new TextView(this);
        tv.setText("Dynamic Text!");
        ll.addView(tv);
 
        // add edit text
        EditText et = new EditText(this);
        et.setText("Dynamic EditText!");
        et.setMinLines(1);
        et.setMaxLines(3);
        ll.addView(et);
 
        // add button
        Button b = new Button(this);
        b.setText("Button added dynamically!");
        b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        b.setId(MY_BUTTON);
        b.setOnClickListener(this);
        ll.addView(b);
 
        //add checkboxes
        for(int i = 0; i < 10; i++) {
            CheckBox cb = new CheckBox(this);
            cb.setText("Dynamic Checkbox " + i);
            cb.setId(i+10);
            ll.addView(cb);
        }
 
        //add radio buttons
        final RadioButton[] rb = new RadioButton[5];
        RadioGroup rg = new RadioGroup(this); //create the RadioGroup
        rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
        for(int i=0; i<5; i++){
            rb[i]  = new RadioButton(this);
            rb[i].setText("Dynamic Radio Button " + i);
            rb[i].setId(i);
            rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
 
        }
        ll.addView(rg);//you add the whole RadioGroup to the layout
        
        // add Toggle button
        ToggleButton tb = new ToggleButton(this);
        tb.setTextOn("Dynamic Toggle Button - ON");
        tb.setTextOff("Dynamic Toggle Button - OFF");
        tb.setChecked(true);
        tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        ll.addView(tb);
 
    }
    
    public void onClick(View v) {
        Toast toast;
        Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId());
        switch (v.getId()) {
        case MY_BUTTON:
            toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 25, 400);
            toast.show();
            saveAnswers();
            break;
            // More buttons go here (if any) ...
 
        }
    
  }
}
公共类ActivityName扩展活动实现OnClickListener
{
专用静态最终int MY_按钮=9000;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
LinearLayout ll=(LinearLayout)findViewById(R.id.LinearLayout 2);
 
//添加文本视图
TextView tv=新的TextView(此);
tv.setText(“动态文本!”);
ll.addView(电视);;
 
//添加编辑文本
EditText et=新的EditText(本);
et.setText(“动态编辑文本!”);
et.setMinLines(1);
et.setMaxLines(3);
ll.addView(et);
 
//添加按钮
按钮b=新按钮(此按钮);
b.setText(“动态添加的按钮!”);
b.setLayoutParams(新的LayoutParams(LayoutParams.WRAP_内容,LayoutParams.WRAP_内容));
b.设置ID(我的按钮);
b.setOnClickListener(本);
ll.addView(b);
 
//添加复选框
对于(int i=0;i<10;i++){
复选框cb=新复选框(此复选框);
cb.setText(“动态复选框”+i);
cb.setId(i+10);
ll.addView(cb);
        }
 
//添加单选按钮
最终单选按钮[]rb=新单选按钮[5];
RadioGroup rg=新的RadioGroup(this);//创建RadioGroup
rg.setOrientation(RadioGroup.HORIZONTAL);//或RadioGroup.VERTICAL

对于(int i=0;iyou需要在
tvOutputArray[]中传递索引。setOnClickListener(this);
将文本视图的侦听器设置为
tvOutputArray[i]。setOnClickListener(this)
当它崩溃时,错误消息是什么?LogCat的o/p是什么?另外,tvOutputArray缺少一个索引。这是一个输入错误,抱歉,但我确实使用了tvOutputArray[i].setOnClickListener(此);在设置侦听器之前是否初始化了所有TexView和按钮?
public class ActivityName extends Activity implements OnClickListener

{

 private static final int MY_BUTTON = 9000;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 
        LinearLayout ll = (LinearLayout)findViewById(R.id.linearLayout2);
 
        // add text view
        TextView tv = new TextView(this);
        tv.setText("Dynamic Text!");
        ll.addView(tv);
 
        // add edit text
        EditText et = new EditText(this);
        et.setText("Dynamic EditText!");
        et.setMinLines(1);
        et.setMaxLines(3);
        ll.addView(et);
 
        // add button
        Button b = new Button(this);
        b.setText("Button added dynamically!");
        b.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        b.setId(MY_BUTTON);
        b.setOnClickListener(this);
        ll.addView(b);
 
        //add checkboxes
        for(int i = 0; i < 10; i++) {
            CheckBox cb = new CheckBox(this);
            cb.setText("Dynamic Checkbox " + i);
            cb.setId(i+10);
            ll.addView(cb);
        }
 
        //add radio buttons
        final RadioButton[] rb = new RadioButton[5];
        RadioGroup rg = new RadioGroup(this); //create the RadioGroup
        rg.setOrientation(RadioGroup.HORIZONTAL);//or RadioGroup.VERTICAL
        for(int i=0; i<5; i++){
            rb[i]  = new RadioButton(this);
            rb[i].setText("Dynamic Radio Button " + i);
            rb[i].setId(i);
            rg.addView(rb[i]); //the RadioButtons are added to the radioGroup instead of the layout
 
        }
        ll.addView(rg);//you add the whole RadioGroup to the layout
        
        // add Toggle button
        ToggleButton tb = new ToggleButton(this);
        tb.setTextOn("Dynamic Toggle Button - ON");
        tb.setTextOff("Dynamic Toggle Button - OFF");
        tb.setChecked(true);
        tb.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
        ll.addView(tb);
 
    }
    
    public void onClick(View v) {
        Toast toast;
        Log.w("ANDROID DYNAMIC VIEWS:", "View Id: " + v.getId());
        switch (v.getId()) {
        case MY_BUTTON:
            toast = Toast.makeText(this, "Clicked on my dynamically added button!", Toast.LENGTH_LONG);
            toast.setGravity(Gravity.TOP, 25, 400);
            toast.show();
            saveAnswers();
            break;
            // More buttons go here (if any) ...
 
        }
    
  }
}