Java 第二次单击返回到原始文本

Java 第二次单击返回到原始文本,java,android,xml,button,Java,Android,Xml,Button,我对Java和xml非常陌生,我遇到了一个问题。我已经创建了一个显示文本的应用程序,当按下按钮时,它会更改文本,我的问题是,当我第二次按下按钮时,我希望文本返回到原始状态 final TextView ad1 = (TextView) findViewById(R.id.t5); final Button b1 = (Button) findViewById(R.id.b1); b1.setOnClickListener(new Vi

我对Java和xml非常陌生,我遇到了一个问题。我已经创建了一个显示文本的应用程序,当按下按钮时,它会更改文本,我的问题是,当我第二次按下按钮时,我希望文本返回到原始状态

        final TextView ad1 = (TextView) findViewById(R.id.t5);
          final Button b1 = (Button) findViewById(R.id.b1);

            b1.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                  // Perform action on click
                   ad1.setText("random text");

                 }
            });
             }

        <TextView
            android:id="@+id/t5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/t4"
            android:layout_margin="5dp"
            android:gravity="center"
            android:text="@string/t5" 
            android:textColor="#ffffff"/>

         <Button
            android:id="@+id/b1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/t5"
            android:layout_centerHorizontal="true"
            android:layout_margin="10dp"
            android:layout_marginTop="14dp"
            android:text="@string/b1"
            android:textColor="#ffffff" />
final TextView ad1=(TextView)findviewbyd(R.id.t5);
最终按钮b1=(按钮)findViewById(R.id.b1);
b1.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
//单击后执行操作
ad1.setText(“随机文本”);
}
});
}
试试这个:

b1.setOnClickListener(new View.OnClickListener()
{
   private String originalText = null;

    public void onClick(View v)
    {
        if (originalText == null) originalText = ad1.getText().toString();

        if (ad1.getText().toString().equals(originalText))
        {
            // Setting a new text
            ad1.setText("random text");
        }
        else
        {
            // Setting back the original text
            ad1.setText(originalText);
        }
    }
});

这就成功了

String newString = "Whoa!";
String lastString;


public void action_Clicked(View view){
    switch(view.getId()){

    case R.id.button1:
        exchange();
        break;

    }
}

private void exchange(){
    lastString = text.getText().toString();
    text.setText(newString);
    newString = lastString;
}

您可能应该删除javascript标记