暴风雪 发表于 2013-1-30 04:07:52

[Android学习]通过dialog模拟spinner,并在底部加上EditText

lz先去喝一碗热翔……
 
国庆前分配的一个囧任务,要自定义一个spinner,并且在spinner下面加上一个用于搜索的EditText。大概效果如下

http://dl.iteye.com/upload/attachment/0074/8284/2c486934-48af-377f-a320-25c4a190c709.jpg
 
    这个spinner真心太奇葩,同学给的资料 http://t.cn/zlWKruo 怪我太弱逼一点都没看懂。后来各种自定义spinner没法做之后决定使用dialog来模拟spinner。
 
然后连着跪了一上午,终于找到了一个解决办法就是,用setItems函数来制作一个spinner,并在底部用setView加上一个TableLayout~~
 
大牛们鄙视我吧,我还是太弱了
 
献上弱代码

package com.example.dialogact;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.os.Bundle;import android.view.LayoutInflater;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {    Button button;    String[] num={"1","2","3","4","5","6","7","8"};    @Override    public void onCreate(Bundle savedInstanceState) {      super.onCreate(savedInstanceState);      setContentView(R.layout.activity_main);         button=(Button)findViewById(R.id.button);      button.setOnClickListener(new OnClickListener() {public void onClick(View v) {// TODO Auto-generated method stubLayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);         View myLoginView = layoutInflater.inflate(R.layout.dlg, null);               new AlertDialog.Builder(MainActivity.this)                .setTitle("选择")                .setItems(num, new DialogInterface.OnClickListener() {                  public void onClick(DialogInterface dialog, int which) {                        button.setText(num);                  Toast info =Toast.makeText(MainActivity.this, num,Toast.LENGTH_LONG);                        info.setMargin(0.0f, 0.3f);                        info.show();                  }                }                )                .setView(myLoginView).create().show();}                });      }} 
Avtivity的布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    >             <Button             android:id="@+id/button"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="xx"             android:layout_weight="1"             ></Button></LinearLayout> 
dialog的布局文件dlg.xml

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    >    <TableRow>      <TextView             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:text="搜索:"             android:textSize="30dp"             android:layout_weight="1"             />                     <EditText             android:id="@+id/text1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:layout_weight="1"             />      </TableRow>></TableLayout> 实现的效果

http://dl.iteye.com/upload/attachment/0074/8294/8c32f2b4-4774-3532-8ae5-1117ea00f7a4.jpg
 
页: [1]
查看完整版本: [Android学习]通过dialog模拟spinner,并在底部加上EditText