我希望我的GridView
只显示16个随机字母,一个4x4图块,如果可能,我不希望字母之间有任何空格。
我还希望它将相应的文本显示为TextView
以形成单词。我现在把它放在一个祝酒词中,以显示它应该如何。但它只显示图像的相应字母。我怎样才能做到这一点?我不知道该怎么办。
任何帮助将不胜感激。
Android编程新手
grid_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="@+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginRight="1dp"
android:layout_marginEnd="1dp"
android:contentDescription="@string/hello_world"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:textSize="15sp"
android:visibility="gone" >
</TextView>
</LinearLayout>
&#13;
activity_main.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="com.grid.gridgame.MainActivity" >
<TextView
android:id="@+id/wordHere"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1234567890"/>
<GridView
android:id="@+id/gridViewCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:columnWidth="80dp"
android:gravity="center"
android:horizontalSpacing="0dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="0dp" />
</RelativeLayout>
&#13;
MainActivity.java
package com.grid.gridgame;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
GridView gridView;
String word;
int rando;
static final String[] setOne = new String[] {
"A", "M","C", "Qu",
"Z", "B", "D", "A",
"E", "I", "E", "L",
"M", "N", "T", "P" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridViewCustom);
gridView.setAdapter(new GridViewCustomAdapter(this, setOne));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(
getApplicationContext(),
((TextView) v.findViewById(R.id.textView))
.getText(), Toast.LENGTH_SHORT).show();
}
});
}
@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;
}
@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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
&#13;
GridViewCustomAdapter
package com.grid.gridgame;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class GridViewCustomAdapter extends BaseAdapter {
private Context context;
private final String[] setOne;
public GridViewCustomAdapter(Context context, String[] setOne) {
this.context = context;
this.setOne = setOne;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
convertView = new ImageView(context);
gridView = new View(context);
// get layout from mobile.xml
gridView = inflater.inflate(R.layout.grid_row, null);
// set value into TextView
TextView textView = (TextView) gridView
.findViewById(R.id.textView);
textView.setText(setOne[position]);
// set image based on selected text
ImageView imageView = (ImageView) gridView
.findViewById(R.id.imageView);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
String allletters = setOne[position];
if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("C")) {
imageView.setImageResource(R.drawable.c);
} else if (allletters.equals("Qu")) {
imageView.setImageResource(R.drawable.qu);
} else if (allletters.equals("Z")) {
imageView.setImageResource(R.drawable.z);
} else if (allletters.equals("B")) {
imageView.setImageResource(R.drawable.b);
} else if (allletters.equals("D")) {
imageView.setImageResource(R.drawable.d);
} else if (allletters.equals("A")) {
imageView.setImageResource(R.drawable.a);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("I")) {
imageView.setImageResource(R.drawable.i);
} else if (allletters.equals("E")) {
imageView.setImageResource(R.drawable.e);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("M")) {
imageView.setImageResource(R.drawable.m);
} else if (allletters.equals("N")) {
imageView.setImageResource(R.drawable.n);
} else if (allletters.equals("T")) {
imageView.setImageResource(R.drawable.t);
} else if (allletters.equals("P")) {
imageView.setImageResource(R.drawable.p);
}
} else {
gridView = (View) convertView;
}
return gridView;
}
@Override
public int getCount() {
return setOne.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
&#13;
答案 0 :(得分:0)
如果你想存储这封信,你可以简单地创建一个字符串并存储用户推送的字母。
Example code:
String word; // word that you want to be display
String letter = MOBILE_OS[id]; // get the corresponding letter from array from the user selected
word.add(letter);