如何设置不同的操作dynamiclly在Android上创建的按钮(特别是 - 使用动态按钮自动滚动到一个TextView) [英] how to set different actions to dynamiclly created button on android (specifically - auto scroll to a textview using a dynamic button)

查看:117
本文介绍了如何设置不同的操作dynamiclly在Android上创建的按钮(特别是 - 使用动态按钮自动滚动到一个TextView)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个包含多个文本视图的布局。
我在一个ArrayList被称为_resultId类变量保存的文本视图的标识。

现在我想创建一个假设滚动到正确的文本视图按钮
(第一个按钮,将第一个文本视图等)

的问题是:如何正确的ID传递给每对preSS方法按钮

我试图用一个全局变量_counter但是当我运行该程序的所有按钮滚动到最后文本视图

该方法的code:

 私人无效addNavigationView(ViewGroup中na​​vigationLayout,ArrayList的< Perek> mishnayot)
{
    的for(int i = 0; I< mishnayot.size();我++)
    {
        _counter = I;
        串currentOt = mishnayot.get(ⅰ).getOt();
        Button按钮=新按钮(getBaseContext());
        button.setText(currentOt);
        如果(_resultId == NULL)
            抛出新的IllegalAccessError(缺失的结果ID链接不能被创建);
        button.setOnClickListener(新OnClickListener()
        {            @覆盖
            公共无效的onClick(查看arg0中)//使其滚动到正确的TextView
            {
                 新的处理程序()。后(新的Runnable()
                 {
                     @覆盖
                     公共无效的run()
                     {
                         查看currentView = findViewById(_resultId.get(_counter));
                         滚动型滚动视图=(滚动型)findViewById(R.id.resultScroll);
                         scrollView.scrollTo(0,currentView.getTop());
                     }
                 });
            }
        });
         navigationLayout.addView(按钮); //将按钮添加到面板
    }
    navigationLayout.setVisibility(View.VISIBLE);
}


解决方案

了解更多Runnable接口上后,我找到了答案所以对于那些将与谁类似的斗争:
你需要创建一个全新的类,同时实现可运行和OnClickListener。

这个类将包含额外的数据和构造以满足您的需求。
当您设置按钮的onclick方法,你创建该类的一个新对象。
例如在我的情况下,新方法看起来像:

 公共类ButtonHandler实现OnClickListener,可运行
{
私人字符串_data; //数据传递的按钮
私人的ArrayList<整数GT; _resultId; //文本意见的id
私人活动_activity;公共ButtonHandler(字符串数据,ArrayList的<整数GT; resultId,活动活动)
{
    _data =数据;
    _resultId = resultId;
    _activity =活动;
}@覆盖
公共无效的run()
{
     查看currentView = _activity.findViewById(_resultId.get(Integer.valueOf(_data)));
     滚动型滚动视图=(滚动型)_activity.findViewById(R.id.resultScroll);
     scrollView.scrollTo(0,currentView.getTop());}@覆盖
公共无效的onClick(视图v)
{
    新的处理程序()后(本)。}}

现在把它从主要活动我使用拨打:

 私人无效addNavigationView(ViewGroup中na​​vigationLayout,ArrayList的< Perek> mishnayot)
{
    的for(int i = 0; I< mishnayot.size();我++)
    {
        _counter = I;
        串currentOt = mishnayot.get(ⅰ).getOt();
        Button按钮=新按钮(getBaseContext());
        button.setText(currentOt);
        如果(_resultId == NULL)
            抛出新的IllegalAccessError(缺失的结果ID链接不能被创建);
        button.setOnClickListener(新ButtonHandler第(i +,_ resultId,此));        navigationLayout.addView(按钮); //将按钮添加到面板
    }
    navigationLayout.setVisibility(View.VISIBLE);
}

i created a layout containing multiple text views. i saved the text view's ids in an ArrayList which is a class variable called _resultId.

now i want to create buttons which suppose to scroll to the correct text view (the first button to the first text view etc)

the question is: how to pass the correct id to each of the buttons on press method?

i tried using a global variable _counter but when i run the program all the buttons scroll to the last text view

the code of the method:

private void addNavigationView(ViewGroup navigationLayout, ArrayList<Perek> mishnayot) 
{
    for (int i=0;i<mishnayot.size();i++)
    {
        _counter=i;
        String currentOt=mishnayot.get(i).getOt();
        Button button = new Button(getBaseContext());
        button.setText(currentOt);
        if (_resultId==null)
            throw new IllegalAccessError("missing result id link cannot be created");
        button.setOnClickListener(new OnClickListener() 
        {

            @Override
            public void onClick(View arg0) //make it scroll to correct textview
            {
                 new Handler().post(new Runnable() 
                 {
                     @Override
                     public void run() 
                     {
                         View currentView=findViewById(_resultId.get(_counter));
                         ScrollView scrollView=(ScrollView) findViewById(R.id.resultScroll);
                         scrollView.scrollTo(0, currentView.getTop());
                     }
                 });
            }
        });
         navigationLayout.addView(button);//add the button to panel
    }
    navigationLayout.setVisibility(View.VISIBLE);
}

解决方案

after learning more on Runnable interface i Found an answer so for those who will struggle with something similar: you need to create a new class altogether that implements both runnable and the OnClickListener.

this class will contain extra data and a constructor to match your needs. when you set the onClick method of the button you create a new object of that class. for instance in my case the new method looked like:

public class ButtonHandler implements OnClickListener,Runnable 
{
private String _data;//data to be passed for the button
private ArrayList<Integer> _resultId;//the id's of the text views
private Activity _activity;

public ButtonHandler(String data,ArrayList<Integer> resultId,Activity activity)
{
    _data=data;
    _resultId=resultId;
    _activity=activity;
}

@Override
public void run() 
{
     View currentView=_activity.findViewById(_resultId.get(Integer.valueOf(_data)));
     ScrollView scrollView=(ScrollView) _activity.findViewById(R.id.resultScroll);
     scrollView.scrollTo(0, currentView.getTop());

}

@Override
public void onClick(View v) 
{
    new Handler().post(this);

}

}

now to call it from the main activity i use:

    private void addNavigationView(ViewGroup navigationLayout, ArrayList<Perek> mishnayot) 
{
    for (int i=0;i<mishnayot.size();i++)
    {
        _counter=i;
        String currentOt=mishnayot.get(i).getOt();
        Button button = new Button(getBaseContext());
        button.setText(currentOt);
        if (_resultId==null)
            throw new IllegalAccessError("missing result id link cannot be created");
        button.setOnClickListener(new ButtonHandler(i+"",_resultId,this));

        navigationLayout.addView(button);//add the button to panel
    }
    navigationLayout.setVisibility(View.VISIBLE);
}

这篇关于如何设置不同的操作dynamiclly在Android上创建的按钮(特别是 - 使用动态按钮自动滚动到一个TextView)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆