我正在尝试创建一个基本的应用程序来获取用户的名字(输入)并显示其名称反转(输出)。但是,我似乎无法获得此代码来显示我想要的输出。我刚刚来到Android。如何在用户输出中显示字符串?
XML:
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Main" >
<Button
android:id="@+id/reverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="166dp"
android:text="@string/reverse" />
<TextView
android:id="@+id/desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp"
android:text="@string/desc"
android:textAppearance="?
android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/desc"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:ems="10"
android:inputType="text">
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/name"
android:layout_alignTop="@+id/reverse"
android:layout_marginTop="67dp"
android:text="@string/reversed"
android:textAppearance="?
android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/displayName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/reverse"
android:layout_below="@+id/textView1"
android:layout_marginTop="46dp"
android:text="@string/medium_text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
爪哇:
package com.example.basicapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Main extends Activity {
Button reverse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class Reverser extends android.app.Activity {
TextView displayed = (TextView)findViewById(R.id.displayName);
EditText nametext = (EditText)findViewById(R.id.name);
String namestring = nametext.getText().toString();
public void onClickListener() {
reverse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String reverse = new StringBuffer(namestring).reverse().toString();
String dr = reverse;
displayed.setText(dr);
}
});
}
}
}
谢谢。
答案 0 :(得分:0)
public class Main extends Activity {
Button reverse;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView displayed = (TextView)findViewById(R.id.displayName);
EditText nametext = (EditText)findViewById(R.id.name);
String namestring = nametext.getText().toString();
reverse=(Button)findViewById(R.id.reverse);//assuming you have button with id reverse
reverse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String reverse = new StringBuffer(namestring).reverse().toString();
String dr = reverse;
displayed.setText(dr);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}