0

非アクティビティ クラスから 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);
    ....
}
4

3 に答える 3

2

findViewByID は静的メソッドではなくインスタンス メソッドであるため、これを行うことはできません。次の 2 つのいずれかを行う必要があります。

1) アクティビティをこのクラスにパラメーターとして渡します。これにより、 findViewByID を呼び出すことができます

2) EditText をパラメーターとして渡します (アクティビティ クラスで findViewByID を呼び出した後)。これにより、 setText を呼び出すだけで済みます

于 2012-11-15T15:39:01.827 に答える
0
EditText et = (EditText) Activity.findViewByID(R.string.te);

アクティビティからアクティビティ var を渡す必要があります。

EditText et = (EditText) (activity var).findViewByID(R.string.te);
于 2012-11-15T15:40:48.143 に答える
0

Edittext アクティビティ クラスのコンテキストを次のように非 keyGenerator クラスに渡すことができます。

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);
于 2012-11-15T15:42:35.390 に答える