java - 从非 Activity 类追加 EditText

标签 java android user-interface view android-activity

我无法将非 Activity 类中的文本字符串附加到 EditText View 。

我尝试将 View 作为参数传递给类的构造函数。

基本问题是我无法在非 Activity 类中使​​用 findViewById。

我知道这是一个愚蠢的问题,但我已经尝试了很多,但根本无法得到答案。

我的代码示例是:

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.printtesting;

import android.app.Activity;
import android.widget.EditText;
import java.math.BigInteger;
import java.util.Random;
import java.util.Vector;
//import javax.swing.JOptionPane;

/**
 *
 * @author HP
 */
public class keyGenerator {

    /**
     * @param args the command line arguments
     */

      static  Vector check = new Vector();


      static protected BigInteger p= new BigInteger("161");
      static protected BigInteger q= new BigInteger("47");
      static protected Random s = new Random();
      static protected BigInteger n = new BigInteger("1");
      static protected BigInteger trails;
      static protected BigInteger lambda ;
      static protected BigInteger nsq  = new BigInteger("1");
      static protected BigInteger g = new BigInteger("1");
      static protected BigInteger temp = new BigInteger("1");

    static protected int maxbit;
    private static BigInteger two = new BigInteger("2");

    public keyGenerator() {
      //  p = new BigInteger(7,1,s);
                System.out.println(p.toString());
      /*  while(!isPrime(p) && ( (p.compareTo(two)==1) || (p.compareTo(two))==0) )
        {
                p = new BigInteger(7,1,s);

        System.out.println(p.toString());
        }*/
        System.out.println("P is " + p);


    //    q = new BigInteger(7,1,s);
      /*  while(!isPrime(q) && ( (q.compareTo(two)==1) || (q.compareTo(two))==0) )
        {
                q = new BigInteger(7,1,s);
        }*/
        System.out.println("Q is " + q);




        // TODO code application logic here

       // BigInteger oth = new BigInteger("132312"); 
       generateKey();

    }


    protected  void generateKey()
    {
   EditText et = (EditText) Activity.findViewByID(R.string.te);
    // N=pq
    n=n.multiply(p);
    n=n.multiply(q);
    ....
}

最佳答案

您不能这样做,因为 findViewByID 是实例方法,而不是静态方法。您需要执行以下两件事之一:

1) 将 Activity 作为参数传递给此类。这将允许您在其上调用 findViewByID

2)将EditText作为参数传递(在 Activity 类中调用findViewByID之后)。这将允许你只调用 setText 就可以了

关于java - 从非 Activity 类追加 EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13400840/

相关文章:

android - 如何检查所有 Realm 实例是否已关闭

android - Github 桌面不显示来自 Android Studio 的更改

android - 从 Android 相机拍摄多张照片后应用程序崩溃

user-interface - 更改 R Shiny 流体页面的背景颜色

使用拖放的 Python GUI 编程,还包含标准输出重定向

java - JSON 日期解析器,以少日形式发布

java - 触发 Activiti 工作流程以执行 ReceiveTask 循环中的任务

java - 结构复杂的JNA

java - SocketChannel 套接字不读取数据

iphone - 基于导航的应用程序中 UITabBar 的替代品