在我的Android应用程序中(差不多完成)我有一个列表视图来显示作弊代码的名称,当用户点击我希望文本更改的名称时
例如:
之前 - 点击我
在你点击我之后
我不确定我是否正确行事,如果OnClick弹出窗口更容易,任何链接或评论/答案都会非常受欢迎
“Android Listview”,“Android Listview弹出窗口”,“Android Listview Onclick”,“Listview Onclick”,“单击时更改Android应用程序文本”,“单击时更改Android列表视图结果”
cheatcodes.xml
<?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:background="@drawable/app_bg"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="20sp" >
</TextView>
</RelativeLayout>
CheatCodes.java
package grand.theft.auto.v;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class CheatCodes extends ListActivity {
static final String[] Cheatcodes = new String[] { "Wanted Level Up", "Wanted Level Down", "Recharge Ability",
"Fast Run", "Fast Swim", "Parachute", "Explosive Punch", "Flaming Bullets",
"Slow Motion Aim", "Sky Fall", "Buzzard Attack Helicopter", "Comet", "Sanchez", "Trash Master",
"Limo", "Stunt Plane", "PCJ-600", "Caddy", "Rapid GT", "Duster"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, R.layout.cheatcodes,Cheatcodes));
ListView listView = getListView();
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(),
((TextView) view).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
答案 0 :(得分:1)
在onItemClick中尝试这个:
CharSequence text= (CharSequence) parent.getItemAtPosition(pos);
//This is where you change the text to show
text = "You " + text;
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
//If you want to update the listview
Cheatcodes[pos] = text;