OnClickListener和textView设置文本

时间:2014-02-03 18:21:21

标签: java android android-layout

我正在尝试动态设置textView的文本,但我既不显示文本也不显示logcat消息。

应用程序启动不会导致任何错误。我尝试使用xml文件中的onClick属性而不是click listener来启动设置文本的方法,但即使这样也行不通,请帮助我。

这是我的代码:

public class Anteprima extends Activity{

 TextView tx;
 Button button;
 ZoomControls zoom;


 @Override
 public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.anteprima);
    tx = (TextView)findViewById(R.id.textView1);
    button = (Button)findViewById(R.id.button1);
    tx.setText("0000000");
    button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
             tx.setText("clickk");
            Log.i("anteprima ","click");

        }});
    targetPath = getIntent().getExtras().getString("target");
     contenutoPath = getIntent().getExtras().getString("contenuto");
    zoom = (ZoomControls)findViewById(R.id.zoomControls1);

    zoom.setIsZoomInEnabled(true);
    zoom.setIsZoomOutEnabled(true);

    zoom.setOnZoomInClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            tx.setText("piu");
        }

    });
    zoom.setOnZoomOutClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            tx.setText("meno");
        }
    });
     }
}

这里是布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button"
     />

<ZoomControls
    android:id="@+id/zoomControls1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="32dp" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="38dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

这是整个活动

package com.tesi.AR;




import com.metaio.sdk.ARViewActivity;
import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKCallback;


public class Anteprima extends ARViewActivity{

public IGeometry mVideo;
 String targetPath;
String contenutoPath;
String AR_FOLDER = "/mnt/sdcard/AR";
float scale = 1.0f;
TextView tx;
Button button;
ZoomControls zoom;
File xmlTemp;

@Override
public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
    setContentView(R.layout.anteprima);
    tx = (TextView)findViewById(R.id.textView1);
    button = (Button)findViewById(R.id.button1);
    tx.setText("0000000");
    button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            tx.setText("clickk");
            Log.i("anteprima ","click");

        }});
    targetPath = getIntent().getExtras().getString("target");
    contenutoPath = getIntent().getExtras().getString("contenuto");
    zoom = (ZoomControls)findViewById(R.id.zoomControls1);

    zoom.setIsZoomInEnabled(true);
     zoom.setIsZoomOutEnabled(true);

    zoom.setOnZoomInClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            tx.setText("piu");
        }

    });
    zoom.setOnZoomOutClickListener(new View.OnClickListener(){
        @Override
         public void onClick(View v) {
            tx.setText("meno");
         }

    });

    Log.i("anteprima", targetPath);
    Log.i("anteprima", contenutoPath);

    //verifica esistenza dello spazio di memoria dedicato all'app
    File arFolder = new File(AR_FOLDER);
    File targetFolder = new File(arFolder, "Targets");
    File contenutiFolder = new File(arFolder, "Contenuti");

    if(!arFolder.exists()){
        Log.i("anteprima", "Cartella non trovata provvedo alla creazione");
        arFolder.mkdirs();
        arFolder.setReadable(true);
        Log.i("anteprima", "Cartella AR creata");
        targetFolder.mkdir(); 
        contenutiFolder.mkdir(); 
    } 
    File targetFakePathFile = new File(targetPath);
    File targetPathFile = new File (targetFolder,targetFakePathFile.getName());
    //provo a creare il nuovo file
    try { 
        targetPathFile.createNewFile();
    } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
    } 
    //Provo a copiare  il file target in targets folder
    try { 
        Utility.copyFile(targetFakePathFile, targetPathFile);
     } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     xmlTemp = new File(targetFolder,"temp.xml");
    //Creo il file di configurazione temporaneo
    try {
        xmlTemp.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
         e.printStackTrace();
    }
    TrackingConfigurationXML tcx = newTrackingConfigurationXML("Nome_Sensore");
    SensorCOS sc = new SensorCOS("idSensore",targetPathFile.getName());

    COS cos = new COS("cos1", "trackingId", sc);

    String xml =   tcx.toStringTheStartXML()+sc.toString()+tcx.toStringTheMiddleXML()+cos.toString()+tcx.toStr    ingTheEndXML();
    BufferedWriter buffWriter = null;
     //scrive sul file xml le impostazioni
    try {
        buffWriter = new BufferedWriter(new FileWriter(xmlTemp));
     } catch (IOException e) {
        // TODO Auto-generated catch block
         e.printStackTrace();
    }
    try {
        buffWriter.write(xml);
        buffWriter.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
     }



}

@Override
protected int getGUILayout() {
    return R.layout.anteprima;
}

@Override
protected IMetaioSDKCallback getMetaioSDKCallbackHandler() {

    return null;
}

@Override
protected void loadContents() {
    String xmlPath = xmlTemp.getAbsolutePath();
    boolean result = metaioSDK.setTrackingConfiguration(xmlPath);
    Log.i("anteprima", "set tarcking conf = "+result);
    mVideo = metaioSDK.createGeometryFromMovie(contenutoPath);
    mVideo.startMovieTexture(true);

}
/*
@Override
public void onDrawFrame() {
    super.onDrawFrame();
    mVideo.setScale(scale);
}
*/
@Override
protected void onGeometryTouched(IGeometry geometry) {
    // TODO Auto-generated method stub

}

}

1 个答案:

答案 0 :(得分:0)

我刚运行你的代码并且工作正常......事实上,我也没有对你的代码做任何改动。你面临的问题是因为代码运行正常..文本视图是动态显示文本按钮点击...