无法在动态按钮上设置文本

时间:2015-12-06 02:04:27

标签: android button settext

我是android新手。 我点击每个按钮,在一排新的表格布局中生成动态按钮。 但是,每当我点击按钮时,我都会在按钮上显示一些文字。 但是我无法为最后一个按钮设置文字(为了清晰起见观察图像)。 请帮帮我朋友。

Please find the image here

这是代码:

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button button = (Button) findViewById(R.id.button1);
    final EditText et=(EditText)findViewById(R.id.wordtxt);
    final String a=et.getText().toString(); 
    final char[] arr=a.toCharArray();   


    // Capture button clicks
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg) {
            //Hiding the Key board
            InputMethodManager inputManager = (InputMethodManager)
                   getSystemService(getApplicationContext().INPUT_METHOD_SERVICE); 
               inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                       InputMethodManager.HIDE_NOT_ALWAYS);

            final TableLayout table=(TableLayout)findViewById(R.id.tbl);


            //Toaster Ex
            int wordCount=et.getText().length();
            Toast.makeText(getApplicationContext(), "Word Count is"+wordCount, 4000).show();

            int k=0;
            final TableRow  tr;
            tr = new TableRow(MainActivity.this);
            table.addView(tr);
            final Button btn;
            btn = new Button(MainActivity.this);
            btn.setText("");
            btn.setId(k);
            tr.addView(btn);
                btn.setOnClickListener(new OnClickListener() {

                    @Override
                   public void onClick(View v) {                        
                        EditText et=(EditText)findViewById(R.id.wordtxt);
                        String a=et.getText().toString(); 
                        char[] arr=a.toCharArray();                         
                        int rowcount= table.getChildCount();                            
                    if(rowcount<arr.length)
                    {
                        if(rowcount==1)
                        {
                                EditText et1=(EditText)findViewById(R.id.wordtxt);
                                String a1=et1.getText().toString(); 
                                char[] arr1=a1.toCharArray();

                                int id=(v.getId());
                                Toast.makeText(getApplicationContext(),"If ID is "+id,2000).show();                         

                                btn.setText(String.valueOf(arr[id]));
                                btn.setEnabled(false);
                                TableRow tr = new TableRow(MainActivity.this);
                                int x=0;
                                int y=0;
                                while(x <rowcount+1)
                                {
                                    if (x % 9 == 0) {
                                      tr = new TableRow(MainActivity.this);                                       
                                      table.addView(tr);
                                    }
                                    Button btn1 = new Button(MainActivity.this);
                                    btn1.setText("");
                                    btn1.setOnClickListener(this);
                                    btn1.setId(x);
                                    tr.addView(btn1);   
                                    x++;                                
                                }
                        }
                        else
                        {

                            EditText et2=(EditText)findViewById(R.id.wordtxt);
                            String a2=et2.getText().toString(); 
                            char[] arr2=a2.toCharArray();                               
                                    int min = 0;
                                    int max = rowcount-1;       
                                    Random r = new Random();
                                    int i1 = r.nextInt(max - min + 1) + min;
                                    int id=(v.getId());
                                    if(id==i1)
                                    {

                                            Button tx = (Button) findViewById(i1);
                                            Button(MainActivity.this);
                                            tx.setText(String.valueOf(arr[i1]));

                                            tx.setEnabled(false);
                                    }

                                    TableRow tr = new TableRow(MainActivity.this);
                                            int x=0;
                                            int y=0;
                                            while(x <rowcount+1)
                                            {                                                       
                                                if (x % 9 == 0) {
                                                  tr = new TableRow(MainActivity.this);                                                   
                                                  table.addView(tr);                                                  
                                                }

                                                Button btn1 = new Button(MainActivity.this);
                                                btn1.setText("");                                                   btn1.setOnClickListener(this);
                                                btn1.setId(x);
                                                tr.addView(btn1);   
                                                x++;                                
                                            }
                        }
                    }


                    }
                });


                table.setShrinkAllColumns(true);

        }
    });


    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        return rootView;
    }
}

}

谢谢。

0 个答案:

没有答案