简体   繁体   English

OnClickListener和textView设置文本

[英]OnClickListener and textView set text

I'm trying to dynamically set the text of my textView but I can't display neither the text nor the logcat message. 我正在尝试动态设置textView的文本,但是我既不显示文本也不显示logcat消息。

The app launch don't cause any errors. 应用启动不会导致任何错误。 I've tried to use the onClick attribute in xml file instead of the click listener to launch a method that set the text but even so doesn't work,please help me. 我尝试使用xml文件中的onClick属性代替click listener来启动设置文本的方法,但即使这样也不起作用,请帮帮我。

Here's my code: 这是我的代码:

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");
        }
    });
     }
}

and here it is the layout 这是布局

<?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>

this is the entire activity 这是整个活动

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

}

}

我刚刚运行了您的代码,它的工作正常……实际上,我也没有对您的代码做任何更改。。由于代码运行正常,您所面临的问题是什么。并且文本视图在Button单击时动态显示文本...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM