[英]Appending an EditText from a non-activity class
我无法将非活动类的文本字符串附加到EditText视图。
我已经尝试将视图作为参数传递给类的构造函数。
基本问题是我无法在非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)将活动作为参数传递给此类。 这将允许您在其上调用findViewByID
2)将EditText作为参数传递(在活动类中调用findViewByID之后)。 这将允许您只需调用setText
EditText et = (EditText) Activity.findViewByID(R.string.te);
您需要从活动中传递活动变量。
EditText et = (EditText) (activity var).findViewByID(R.string.te);
您可以将Edittext Activity Class的Context传递给Non keyGenerator Class,如下所示:
Context conn;
public keyGenerator(Context con) {
conn=con;
//Your Code...
}
现在,您可以将keyGenerator上的文本附加到Edittext,如下所示:
EditText et = (EditText)conn.findViewByID(R.string.te);
et.append("your text here");
您可以将当前上下文传递给keyGenerator构造函数,如下所示:
keyGenerator obj=new keyGenerator(Current_Activity.this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.