在代码中编辑TextView对象

时间:2014-11-08 08:14:23

标签: java android xml

我终于有了一个想法来测试我的Android应用程序开发知识。事实证明,Android开发非常复杂,但一旦你理解了事物的流动就会变得容易。无论如何,我认为'我通过XML和Java正确设置了所有内容,但TextView对象由于某种原因不接受我给它们的值,或者我正在调用以错误方式编辑它们的方法。我不知道它是哪一个,这就是我在这里发帖的原因。我也通过链接检查了许多其他主题,但是我们不知道我做错了什么,或者完全忽略了这一点。

  Activity Popup (Main)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".Popup"
tools:ignore="MergeRootFrame" >

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left|top"
    android:weightSum="1"
    android:id="@+id/LyOutInstructions">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Instructions:"
        android:id="@+id/txtInstruction"
        android:autoText="false"
        android:textStyle="bold"
        android:typeface="monospace" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Mov"
        android:id="@+id/btnMov"
        android:onClick="onClick"
        android:enabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add"
        android:id="@+id/btnAdd"
        android:onClick="onClick"
        android:enabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sub"
        android:id="@+id/btnSub"
        android:onClick="onClick"
        android:enabled="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Int"
        android:id="@+id/btnInt"
        android:onClick="onClick"
        android:enabled="true" />

</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|top"
    android:id="@+id/LyOutRegisters">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Registers:"
        android:id="@+id/txtRegisters"
        android:textStyle="bold" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EAX"
        android:id="@+id/btnRegEAX"
        android:onClick="onClick"
        android:enabled="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EBX"
        android:id="@+id/btnRegEBX"
        android:onClick="onClick"
        android:enabled="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ECX"
        android:id="@+id/btnRegECX"
        android:onClick="onClick"
        android:enabled="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EDX"
        android:id="@+id/btnRegEDX"
        android:onClick="onClick"
        android:enabled="false" />

</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="250dp"
    android:layout_gravity="left|bottom"
    android:id="@+id/LyOutRegisterNames">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EAX:"
        android:id="@+id/txtRegNameEAX" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EBX:"
        android:id="@+id/txtRegNameEBX" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ECX:"
        android:id="@+id/txtRegNameECX" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EDX:"
        android:id="@+id/txtRegNameEDX" />
</LinearLayout>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="282dp"
    android:layout_height="250dp"
    android:layout_gravity="center_horizontal|bottom"
    android:id="@+id/LyOutRegisterOutput">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtRegValueEAX"
        android:text="@string/Instruction_EAX"
        android:editable="false"
        android:enabled="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtRegValueEBX"
        android:text="@string/Instruction_EBX"
        android:editable="false"
        android:enabled="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtRegValueECX"
        android:text="@string/Instruction_ECX"
        android:editable="false"
        android:enabled="true" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtRegValueEDX"
        android:text="@string/Instruction_EDX"
        android:editable="true"/>
</LinearLayout>

GetData (Popup)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:id="@+id/DataLayout"
android:layout_height="250dp"
android:background="@drawable/pop"
android:orientation="vertical"
android:gravity="center">

<TextView
    android:id="@+id/GetDataTitle"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter your Data" />

<EditText
    android:id="@+id/GetDataInstruction"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:hint="@string/GetData"
    android:inputType="text|textNoSuggestions" />

<Button
    android:id="@+id/btnEnterData"
    android:layout_marginTop="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter Data"
    android:layout_gravity="center_horizontal" />

strings

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">Basic Assembler</string>
<string name="Instruction_EAX"> </string>
<string name="Instruction_EBX"> </string>
<string name="Instruction_ECX"> </string>
<string name="Instruction_EDX"> </string>
<string name="GetData">Please enter your Data...</string>
<string name="title">Assemble</string>
<string name="app_settings">Settings</string>

Popup.java
package com.wtf.demo;
import com.wtf.demo.GetData;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import android.content.Intent;


public class Popup extends Activity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_popup);
    if (savedInstanceState == null) {
    }
}

public void onClick(View view) {
    int id = view.getId();
    Intent ShowDialog = new Intent(Popup.this, GetData.class);
    if (id == R.id.btnAdd) {
        Data.Instruction = "add";
        startActivity(ShowDialog);
    } else if (id == R.id.btnSub) {
        Data.Instruction = "sub";
        startActivity(ShowDialog);
    } else if (id == R.id.btnMov) {
        Data.Instruction = "mov";
        startActivity(ShowDialog);
    } else if (id == R.id.btnInt) {
        Data.Instruction = "int";
        startActivity(ShowDialog);
    }
    Update();
}

private void Update() {
    new Tools(Data.Instruction, Data.CachedData);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_popup, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public class Tools {
    public Tools(Object X, Object Y) {
        Validate(X + " " + Y);
    }

    private void Validate(Object _Data) {
        int i;
        if (_Data != null && !_Data.toString().equals(" ")) {
            String[] Temp = _Data.toString().split(",");
            if (Temp.length > 2) {
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(1);
            } else {
                String Value = null;
                boolean FirstValue = false;
                for (i = 0; i < Temp.length; i++) {
                    String _Value = Temp[i].trim().toLowerCase();
                    if (!_Value.equals(" ")) {
                        if (Arrays.asList(Data.Registers).contains(_Value)) {
                            if (!FirstValue) {
                                FirstValue = true;
                                Value = _Value;
                            }
                        }
                    }
                }
                if (Value != null) {
                    Commit((Object) Temp[0], (Object) Temp[1], Value);
                }
            }
        }
    }

    private void Commit(Object X, Object Y, String Value) {
        TextView txtRegEAX, txtRegEBX, txtRegECX, txtRegEDX;
        txtRegEAX = (TextView) findViewById(R.id.txtRegValueEAX);
        txtRegEBX = (TextView) findViewById(R.id.txtRegValueEBX);
        txtRegECX = (TextView) findViewById(R.id.txtRegValueECX);
        txtRegEDX = (TextView) findViewById(R.id.txtRegValueEDX);
        //Int Destination, Source : Intel Syntax
        if (Value.equals(X)){
            if (Arrays.asList(Data.Registers).contains(Y)){
                if (Value.equals("eax")){ _Commit(Data.EAX, Y); }
                else if (Value.equals("ebx")){ _Commit(Data.EBX, Y); }
                else if (Value.equals("ecx")){ _Commit(Data.ECX, Y); }
                else if (Value.equals("edx")){ _Commit(Data.EDX, Y); }
            }
            else {
                if (Value.equals("eax")){ Commit(Data.EAX, (Integer)Y); }
                else if (Value.equals("ebx")){ Commit(Data.EBX, (Integer)Y); }
                else if (Value.equals("ecx")){ Commit(Data.ECX, (Integer)Y); }
                else if (Value.equals("edx")){ Commit(Data.EDX, (Integer)Y); }
            }
        }
        txtRegEAX.setText(String.valueOf(Data.EAX));
        txtRegEBX.setText(String.valueOf(Data.EBX));
        txtRegECX.setText(String.valueOf(Data.ECX));
        txtRegEDX.setText(String.valueOf(Data.EDX));
    }

    private void _Commit(Object X, Object Y) {
        if (Y.equals("eax")) Commit(X, Data.EAX);
        else if (Y.equals("ebx")) Commit(X, Data.EBX);
        else if (Y.equals("ecx")) Commit(X, Data.ECX);
        else if (Y.equals("edx")) Commit(X, Data.EDX);
    }

    private void Commit(Object X, int Y) {
        int x = (Integer)X;
        if (Data.Instruction.equals("mov")) X = Y + 0;
        else if (Data.Instruction.equals("add")) X = x + Y;
        else if (Data.Instruction.equals("sub")) X = x - Y;
        else if (Data.Instruction.equals("int")) X = x + 0;
    }
}

public static class Data {
    public static int EAX = 0;
    public static int EBX = 0;
    public static int ECX = 0;
    public static int EDX = 0;
    public static String Instruction = null;
    public static String CachedData = null;
    public static String[] Registers = new String[]{"eax", "ebx", "ecx", "edx"};
}
}

GetData.java
package com.wtf.demo;

import android.app.Activity;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.view.Gravity;
import com.wtf.demo.Popup;

/**
 * Created by Ghost on 11/4/2014.
 */
public class GetData extends Activity {
EditText GetData;
Button EnterData;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.getdata_popup);
    final Button btnEnterData = (Button) findViewById(R.id.btnEnterData);
    GetData = (EditText) findViewById(R.id.GetDataInstruction);
    btnEnterData.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            String Cache = GetData.getText().toString();
            if (Cache != null) {
                Popup.Data.CachedData = Cache;
                Done();
            }
            else finish();
        }
    });
}

private void Done() {
    finish();
}
}

编辑#1:我刚刚意识到由于某种原因我的复制/粘贴搞砸了,我发布了两次activity_popup.xml,我现在正处于调试过程中。为什么选择downvote?

Edut#2:如果你不管理你的活动,你最终会得到基本的意大利面条代码,而不是链接和丑陋。我可以公开我的更新方法,但这不会解决这个问题,并会加强前者。当我完成正确的重构时会更新。

2 个答案:

答案 0 :(得分:0)

您的布局中没有EditText,您无法对textview执行任何操作,在运行应用时使用edittext进行编辑

答案 1 :(得分:0)

txtRegEAX.setText()应该可以工作,所以我建议你输入一些断点,以确定值是否为空或代码是否已运行。请告诉我你的想法。